Skip to content

Commit 245c3db

Browse files
andersy005dcherian
andauthored
Enable explicit use of key tuples (instead of *Indexer objects) in indexing adapters and explicitly indexed arrays (#8870)
* pass key tuple to indexing adapters and explicitly indexed arrays * update indexing in StackedBytesArray * Update indexing in StackedBytesArray * Add _IndexerKey type to _typing.py * Update indexing in StackedBytesArray * use tuple indexing in test_backend_array_deprecation_warning * Add support for CompatIndexedTuple in explicit indexing adapter This commit updates the `explicit_indexing_adapter` function to accept both `ExplicitIndexer` and the new `CompatIndexedTuple`. The `CompatIndexedTuple` is designed to facilitate the transition towards using raw tuples by carrying additional metadata about the indexing type (basic, vectorized, or outer). * remove unused code * type hint fixes * fix docstrings * fix tests * fix docstrings * Apply suggestions from code review Co-authored-by: Deepak Cherian <dcherian@users.noreply.github.com> * update docstrings and pass tuples directly * Some test cleanup * update docstring * use `BasicIndexer` instead of `CompatIndexedTuple` * support explicit indexing with tuples * fix mypy errors * remove unused IndexerMaker * Update LazilyIndexedArray._updated_key to support explicit indexing with tuples --------- Co-authored-by: Deepak Cherian <dcherian@users.noreply.github.com> Co-authored-by: Deepak Cherian <deepak@cherian.net>
1 parent 79cfe4d commit 245c3db

File tree

9 files changed

+212
-199
lines changed

9 files changed

+212
-199
lines changed

xarray/coding/strings.py

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
from xarray.core import indexing
1818
from xarray.core.utils import module_available
1919
from xarray.core.variable import Variable
20+
from xarray.namedarray._typing import _IndexerKey
2021
from xarray.namedarray.parallelcompat import get_chunked_array_type
2122
from xarray.namedarray.pycompat import is_chunked_array
2223

@@ -220,8 +221,7 @@ class StackedBytesArray(indexing.ExplicitlyIndexedNDArrayMixin):
220221
"""Wrapper around array-like objects to create a new indexable object where
221222
values, when accessed, are automatically stacked along the last dimension.
222223
223-
>>> indexer = indexing.BasicIndexer((slice(None),))
224-
>>> StackedBytesArray(np.array(["a", "b", "c"], dtype="S1"))[indexer]
224+
>>> StackedBytesArray(np.array(["a", "b", "c"], dtype="S1"))[(slice(None),)]
225225
array(b'abc', dtype='|S3')
226226
"""
227227

@@ -240,7 +240,7 @@ def __init__(self, array):
240240

241241
@property
242242
def dtype(self):
243-
return np.dtype("S" + str(self.array.shape[-1]))
243+
return np.dtype(f"S{str(self.array.shape[-1])}")
244244

245245
@property
246246
def shape(self) -> tuple[int, ...]:
@@ -249,15 +249,17 @@ def shape(self) -> tuple[int, ...]:
249249
def __repr__(self):
250250
return f"{type(self).__name__}({self.array!r})"
251251

252-
def _vindex_get(self, key):
252+
def _vindex_get(self, key: _IndexerKey):
253253
return _numpy_char_to_bytes(self.array.vindex[key])
254254

255-
def _oindex_get(self, key):
255+
def _oindex_get(self, key: _IndexerKey):
256256
return _numpy_char_to_bytes(self.array.oindex[key])
257257

258-
def __getitem__(self, key):
258+
def __getitem__(self, key: _IndexerKey):
259+
from xarray.core.indexing import BasicIndexer
260+
259261
# require slicing the last dimension completely
260-
key = type(key)(indexing.expanded_indexer(key.tuple, self.array.ndim))
261-
if key.tuple[-1] != slice(None):
262+
indexer = indexing.expanded_indexer(key, self.array.ndim)
263+
if indexer[-1] != slice(None):
262264
raise IndexError("too many indices")
263-
return _numpy_char_to_bytes(self.array[key])
265+
return _numpy_char_to_bytes(self.array[BasicIndexer(indexer)])

xarray/coding/variables.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -99,8 +99,7 @@ class NativeEndiannessArray(indexing.ExplicitlyIndexedNDArrayMixin):
9999
>>> NativeEndiannessArray(x).dtype
100100
dtype('int16')
101101
102-
>>> indexer = indexing.BasicIndexer((slice(None),))
103-
>>> NativeEndiannessArray(x)[indexer].dtype
102+
>>> NativeEndiannessArray(x)[(slice(None),)].dtype
104103
dtype('int16')
105104
"""
106105

@@ -137,8 +136,7 @@ class BoolTypeArray(indexing.ExplicitlyIndexedNDArrayMixin):
137136
>>> BoolTypeArray(x).dtype
138137
dtype('bool')
139138
140-
>>> indexer = indexing.BasicIndexer((slice(None),))
141-
>>> BoolTypeArray(x)[indexer].dtype
139+
>>> BoolTypeArray(x)[(slice(None),)].dtype
142140
dtype('bool')
143141
"""
144142

0 commit comments

Comments
 (0)