Skip to content
forked from pydata/xarray

Commit 91642c7

Browse files
committed
Fix negative slicing of Zarr arrays
Closes pydata#8252 Closes pydata#3921
1 parent ca4f121 commit 91642c7

File tree

2 files changed

+8
-9
lines changed

2 files changed

+8
-9
lines changed

xarray/backends/zarr.py

+8-1
Original file line numberDiff line numberDiff line change
@@ -86,10 +86,17 @@ def get_array(self):
8686
def _oindex(self, key):
8787
return self._array.oindex[key]
8888

89+
def _getitem(self, key):
90+
return self._array[key]
91+
8992
def __getitem__(self, key):
9093
array = self._array
9194
if isinstance(key, indexing.BasicIndexer):
92-
return array[key.tuple]
95+
# this will convert negative slices to positive slices
96+
# The latter are all that Zarr supports
97+
return indexing.explicit_indexing_adapter(
98+
key, array.shape, indexing.IndexingSupport.VECTORIZED, self._getitem
99+
)
93100
elif isinstance(key, indexing.VectorizedIndexer):
94101
return array.vindex[
95102
indexing._arrayize_vectorized_indexer(key, self.shape).tuple

xarray/tests/test_backends.py

-8
Original file line numberDiff line numberDiff line change
@@ -2929,14 +2929,6 @@ def test_attributes(self, obj) -> None:
29292929
with pytest.raises(TypeError, match=r"Invalid attribute in Dataset.attrs."):
29302930
ds.to_zarr(store_target, **self.version_kwargs)
29312931

2932-
def test_vectorized_indexing_negative_step(self) -> None:
2933-
if not has_dask:
2934-
pytest.xfail(
2935-
reason="zarr without dask handles negative steps in slices incorrectly"
2936-
)
2937-
2938-
super().test_vectorized_indexing_negative_step()
2939-
29402932

29412933
@requires_zarr
29422934
class TestZarrDictStore(ZarrBase):

0 commit comments

Comments
 (0)