Skip to content

Commit b3eb68b

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

File tree

3 files changed

+10
-9
lines changed

3 files changed

+10
-9
lines changed

doc/whats-new.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,8 @@ Bug fixes
4444
By `Tom Nicholas <https://github.com/TomNicholas>`_.
4545
- Ensure :py:meth:`DataArray.unstack` works when wrapping array API-compliant classes. (:issue:`8666`, :pull:`8668`)
4646
By `Tom Nicholas <https://github.com/TomNicholas>`_.
47+
- Fix negative slicing of Zarr arrays without dask installed. (:issue:`8252`)
48+
By `Deepak Cherian <https://github.com/dcherian>`_.
4749

4850
Documentation
4951
~~~~~~~~~~~~~

xarray/backends/zarr.py

Lines changed: 8 additions & 1 deletion
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

Lines changed: 0 additions & 8 deletions
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)