Skip to content

Fix for scalar detection #8821

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

Merged
merged 8 commits into from
May 6, 2025
Merged
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
9 changes: 8 additions & 1 deletion xarray/core/indexing.py
Original file line number Diff line number Diff line change
Expand Up @@ -769,7 +769,14 @@ def __repr__(self) -> str:

def _wrap_numpy_scalars(array):
"""Wrap NumPy scalars in 0d arrays."""
if np.isscalar(array):
if np.ndim(array) == 0 and (
isinstance(array, np.generic)
or not (is_duck_array(array) or isinstance(array, NDArrayMixin))
):
return np.array(array)
elif hasattr(array, "dtype"):
return array
elif np.ndim(array) == 0:
return np.array(array)
else:
return array
Expand Down
30 changes: 30 additions & 0 deletions xarray/tests/test_backends.py
Original file line number Diff line number Diff line change
Expand Up @@ -2276,6 +2276,36 @@ def test_write_inconsistent_chunks(self) -> None:
def test_roundtrip_coordinates(self) -> None:
super().test_roundtrip_coordinates()

@requires_cftime
def test_roundtrip_cftime_bnds(self):
# Regression test for issue #7794
import cftime

original = xr.Dataset(
{
"foo": ("time", [0.0]),
"time_bnds": (
("time", "bnds"),
[
[
cftime.Datetime360Day(2005, 12, 1, 0, 0, 0, 0),
cftime.Datetime360Day(2005, 12, 2, 0, 0, 0, 0),
]
],
),
},
{"time": [cftime.Datetime360Day(2005, 12, 1, 12, 0, 0, 0)]},
)

with create_tmp_file() as tmp_file:
original.to_netcdf(tmp_file)
with open_dataset(tmp_file) as actual:
# Operation to load actual time_bnds into memory
assert_array_equal(actual.time_bnds.values, original.time_bnds.values)
chunked = actual.chunk(time=1)
with create_tmp_file() as tmp_file_chunked:
chunked.to_netcdf(tmp_file_chunked)


@requires_zarr
@pytest.mark.usefixtures("default_zarr_format")
Expand Down
Loading