Skip to content

Commit 41dfa16

Browse files
committed
BUG: Fix display with nested arrays
Currently, the following will fail to display: ```python import xarray as xr import numpy as np x = np.empty((2, 2), dtype=object) for i in range(2): for j in range(2): x[i, j] = np.zeros(2) # Set to 1D array of size 2 ds = xr.DataArray(x) ds ``` Whenever there are `size==1` arrays, it currently does work: ```python import xarray as xr import numpy as np x = np.empty((2, 2), dtype=object) for i in range(2): for j in range(2): x[i, j] = np.zeros((1, 1, 1)) # Set to 3D array of size 1 ds = xr.DataArray(x) ds For context, I am running into this issue in https://github.com/pipefunc/pipefunc where one can put the resulting objects into `xarray.Dataset`s. For example, see the docs here https://pipefunc.readthedocs.io/en/latest/examples/physics-simulation/ (search for `xarray.Dataset`).
1 parent aa9e2bd commit 41dfa16

File tree

2 files changed

+3
-1
lines changed

2 files changed

+3
-1
lines changed

xarray/core/formatting.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -184,7 +184,7 @@ def format_item(x, timedelta_format=None, quote_strings=True):
184184
if hasattr(x, "dtype"):
185185
x = x.item()
186186
return repr(x) if quote_strings else x
187-
elif hasattr(x, "dtype") and np.issubdtype(x.dtype, np.floating):
187+
elif hasattr(x, "dtype") and np.issubdtype(x.dtype, np.floating) and x.size == 1:
188188
return f"{x.item():.4}"
189189
else:
190190
return str(x)

xarray/tests/test_formatting.py

+2
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,8 @@ def test_format_item(self) -> None:
101101
(np.float16(1.1234), "1.123"),
102102
(np.float32(1.0111111), "1.011"),
103103
(np.float64(22.222222), "22.22"),
104+
(np.zeros((1, 1)), "array([[0.]])"),
105+
(np.zeros(2), "array([0., 0.])"),
104106
]
105107
for item, expected in cases:
106108
actual = formatting.format_item(item)

0 commit comments

Comments
 (0)