Skip to content

Commit 28bbf96

Browse files
committed
Add small test exposing issue from #7794. Propose fix for _wrap_numpy_scalars
1 parent 90e00f0 commit 28bbf96

File tree

2 files changed

+23
-0
lines changed

2 files changed

+23
-0
lines changed

xarray/core/indexing.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -686,6 +686,10 @@ def _wrap_numpy_scalars(array):
686686
"""Wrap NumPy scalars in 0d arrays."""
687687
if np.isscalar(array):
688688
return np.array(array)
689+
elif hasattr(array, "dtype"):
690+
return array
691+
elif np.ndim(array) == 0:
692+
return np.array(array)
689693
else:
690694
return array
691695

xarray/tests/test_backends.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2060,6 +2060,25 @@ def test_write_inconsistent_chunks(self) -> None:
20602060
assert actual["x"].encoding["chunksizes"] == (50, 100)
20612061
assert actual["y"].encoding["chunksizes"] == (100, 50)
20622062

2063+
@requires_cftime
2064+
def test_roundtrip_cftime_bnds(self):
2065+
# Regression test for issue #7794
2066+
import cftime
2067+
original = xr.Dataset(
2068+
{"foo": ("time", [0.]),
2069+
"time_bnds": (("time","bnds"), [[cftime.Datetime360Day(2005, 12, 1, 0, 0, 0, 0),
2070+
cftime.Datetime360Day(2005, 12, 2, 0, 0, 0, 0)]])},
2071+
{"time": [cftime.Datetime360Day(2005, 12, 1, 12, 0, 0, 0)]})
2072+
2073+
2074+
with create_tmp_file() as tmp_file:
2075+
original.to_netcdf(tmp_file)
2076+
with open_dataset(tmp_file) as actual:
2077+
tb = actual.time_bnds.values
2078+
chunked = actual.chunk(time=1)
2079+
with create_tmp_file() as tmp_file_chunked:
2080+
chunked.to_netcdf(tmp_file_chunked)
2081+
20632082

20642083
@requires_zarr
20652084
class ZarrBase(CFEncodedBase):

0 commit comments

Comments
 (0)