diff --git a/xarray/core/formatting.py b/xarray/core/formatting.py index 993cddf2b57..7088bd40a06 100644 --- a/xarray/core/formatting.py +++ b/xarray/core/formatting.py @@ -184,7 +184,7 @@ def format_item(x, timedelta_format=None, quote_strings=True): if hasattr(x, "dtype"): x = x.item() return repr(x) if quote_strings else x - elif hasattr(x, "dtype") and np.issubdtype(x.dtype, np.floating): + elif hasattr(x, "dtype") and np.issubdtype(x.dtype, np.floating) and x.size == 1: return f"{x.item():.4}" else: return str(x) diff --git a/xarray/tests/test_formatting.py b/xarray/tests/test_formatting.py index 2e0925c1b9a..dce1528c7ae 100644 --- a/xarray/tests/test_formatting.py +++ b/xarray/tests/test_formatting.py @@ -101,6 +101,9 @@ def test_format_item(self) -> None: (np.float16(1.1234), "1.123"), (np.float32(1.0111111), "1.011"), (np.float64(22.222222), "22.22"), + (np.zeros((1, 1)), "0.0"), + (np.zeros(2), "[0. 0.]"), + (np.zeros((2, 2)), "[[0. 0.]\n [0. 0.]]"), ] for item, expected in cases: actual = formatting.format_item(item)