Skip to content

BUG: Fix display with nested NumPy arrays #10222

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion xarray/core/formatting.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
3 changes: 3 additions & 0 deletions xarray/tests/test_formatting.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"),
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is the original behavior.

I would argue that this is technically incorrect.

As an alternative we could check if x.shape == ().

(np.zeros(2), "[0. 0.]"),
(np.zeros((2, 2)), "[[0. 0.]\n [0. 0.]]"),
]
for item, expected in cases:
actual = formatting.format_item(item)
Expand Down
Loading