Skip to content

Commit 99a1ad2

Browse files
authored
BUG: Fix display with nested NumPy arrays (pydata#10222)
1 parent 24e83ac commit 99a1ad2

File tree

3 files changed

+5
-2
lines changed

3 files changed

+5
-2
lines changed

xarray/core/formatting.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -190,7 +190,7 @@ def format_item(x, timedelta_format=None, quote_strings=True):
190190
if hasattr(x, "dtype"):
191191
x = x.item()
192192
return repr(x) if quote_strings else x
193-
elif hasattr(x, "dtype") and np.issubdtype(x.dtype, np.floating):
193+
elif hasattr(x, "dtype") and np.issubdtype(x.dtype, np.floating) and x.shape == ():
194194
return f"{x.item():.4}"
195195
else:
196196
return str(x)

xarray/tests/test_formatting.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,9 @@ 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)), "[[0.]]"),
105+
(np.zeros(2), "[0. 0.]"),
106+
(np.zeros((2, 2)), "[[0. 0.]\n [0. 0.]]"),
104107
]
105108
for item, expected in cases:
106109
actual = formatting.format_item(item)

xarray/tests/test_plot.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -828,7 +828,7 @@ def test_slice_in_title_single_item_array(self) -> None:
828828
darray = self.darray.expand_dims({"d": np.array([10.009])})
829829
darray.plot.line(x="period")
830830
title = plt.gca().get_title()
831-
assert "d = 10.01" == title
831+
assert "d = [10.009]" == title
832832

833833

834834
class TestPlotStep(PlotTestCase):

0 commit comments

Comments
 (0)