From 6187d80144f1b029853fff8e74cc5dd4115dd349 Mon Sep 17 00:00:00 2001 From: Deepak Cherian Date: Fri, 9 Feb 2024 19:57:32 -0700 Subject: [PATCH] Fix negative slicing of Zarr arrays (#8674) * Fix negative slicing of Zarr arrays Closes #8252 Closes #3921 * Cleanup --- doc/whats-new.rst | 2 ++ xarray/backends/zarr.py | 22 +++++++++++++--------- 2 files changed, 15 insertions(+), 9 deletions(-) diff --git a/doc/whats-new.rst b/doc/whats-new.rst index b2c4a0b5257..d51b5da2a88 100644 --- a/doc/whats-new.rst +++ b/doc/whats-new.rst @@ -59,6 +59,8 @@ Bug fixes By `Tom Nicholas `_. - Ensure :py:meth:`DataArray.unstack` works when wrapping array API-compliant classes. (:issue:`8666`, :pull:`8668`) By `Tom Nicholas `_. +- Fix negative slicing of Zarr arrays without dask installed. (:issue:`8252`) + By `Deepak Cherian `_. - Preserve chunks when writing time-like variables to zarr by enabling lazy CF encoding of time-like variables (:issue:`7132`, :issue:`8230`, :issue:`8432`, :pull:`8575`). By `Spencer Clark `_ and diff --git a/xarray/backends/zarr.py b/xarray/backends/zarr.py index 469bbf4c339..8f9040477d9 100644 --- a/xarray/backends/zarr.py +++ b/xarray/backends/zarr.py @@ -86,19 +86,23 @@ def get_array(self): def _oindex(self, key): return self._array.oindex[key] + def _vindex(self, key): + return self._array.vindex[key] + + def _getitem(self, key): + return self._array[key] + def __getitem__(self, key): array = self._array if isinstance(key, indexing.BasicIndexer): - return array[key.tuple] + method = self._getitem elif isinstance(key, indexing.VectorizedIndexer): - return array.vindex[ - indexing._arrayize_vectorized_indexer(key, self.shape).tuple - ] - else: - assert isinstance(key, indexing.OuterIndexer) - return indexing.explicit_indexing_adapter( - key, array.shape, indexing.IndexingSupport.VECTORIZED, self._oindex - ) + method = self._vindex + elif isinstance(key, indexing.OuterIndexer): + method = self._oindex + return indexing.explicit_indexing_adapter( + key, array.shape, indexing.IndexingSupport.VECTORIZED, method + ) # if self.ndim == 0: # could possibly have a work-around for 0d data here