diff --git a/xarray/core/indexing.py b/xarray/core/indexing.py index aa56006eff3..a0053b0c026 100644 --- a/xarray/core/indexing.py +++ b/xarray/core/indexing.py @@ -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 diff --git a/xarray/tests/test_backends.py b/xarray/tests/test_backends.py index 1d9c90b37b1..95c53786f86 100644 --- a/xarray/tests/test_backends.py +++ b/xarray/tests/test_backends.py @@ -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") pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy