Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enable explicit use of key tuples (instead of *Indexer objects) in indexing adapters and explicitly indexed arrays #8870

Merged
Show file tree
Hide file tree
Changes from 17 commits
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
4465699
pass key tuple to indexing adapters and explicitly indexed arrays
andersy005 Mar 23, 2024
728d57f
update indexing in StackedBytesArray
andersy005 Mar 23, 2024
6b81669
Merge branch 'main' into pass-key-tuple-to-indexing-adapters
andersy005 Mar 26, 2024
b68b32b
Update indexing in StackedBytesArray
andersy005 Mar 26, 2024
0322733
Merge branch 'main' into pass-key-tuple-to-indexing-adapters
andersy005 Mar 27, 2024
1b8f810
Add _IndexerKey type to _typing.py
andersy005 Mar 27, 2024
c3c7cce
Merge branch 'backend-indexing' into pass-key-tuple-to-indexing-adapters
andersy005 Apr 10, 2024
3a3d42f
Merge branch 'backend-indexing' into pass-key-tuple-to-indexing-adapters
andersy005 Apr 10, 2024
07a14b8
Merge branch 'backend-indexing' into pass-key-tuple-to-indexing-adapters
andersy005 Apr 30, 2024
52c0f4c
Update indexing in StackedBytesArray
andersy005 Apr 30, 2024
3c76fc5
use tuple indexing in test_backend_array_deprecation_warning
andersy005 Apr 30, 2024
f062d0d
Add support for CompatIndexedTuple in explicit indexing adapter
andersy005 Apr 30, 2024
fb17dc2
remove unused code
andersy005 Apr 30, 2024
5fa48eb
type hint fixes
andersy005 Apr 30, 2024
cdd605f
fix docstrings
andersy005 Apr 30, 2024
f35a72b
fix tests
andersy005 Apr 30, 2024
e646bbf
fix docstrings
andersy005 Apr 30, 2024
2b273ab
Apply suggestions from code review
andersy005 Apr 30, 2024
237bbe2
update docstrings and pass tuples directly
andersy005 Apr 30, 2024
a917807
Some test cleanup
dcherian Apr 30, 2024
bae5fbd
update docstring
andersy005 May 2, 2024
acd7f6c
Merge branch 'backend-indexing' into pass-key-tuple-to-indexing-adapters
andersy005 May 2, 2024
203849a
use `BasicIndexer` instead of `CompatIndexedTuple`
andersy005 May 2, 2024
ab0db23
support explicit indexing with tuples
andersy005 May 2, 2024
8f74b18
fix mypy errors
andersy005 May 2, 2024
3ee14aa
remove unused IndexerMaker
andersy005 May 2, 2024
303b2e6
Update LazilyIndexedArray._updated_key to support explicit indexing w…
andersy005 May 2, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 11 additions & 8 deletions xarray/coding/strings.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
from xarray.core import indexing
from xarray.core.utils import module_available
from xarray.core.variable import Variable
from xarray.namedarray._typing import _IndexerKey
from xarray.namedarray.parallelcompat import get_chunked_array_type
from xarray.namedarray.pycompat import is_chunked_array

Expand Down Expand Up @@ -221,7 +222,7 @@ class StackedBytesArray(indexing.ExplicitlyIndexedNDArrayMixin):
values, when accessed, are automatically stacked along the last dimension.

>>> indexer = indexing.BasicIndexer((slice(None),))
>>> StackedBytesArray(np.array(["a", "b", "c"], dtype="S1"))[indexer]
>>> StackedBytesArray(np.array(["a", "b", "c"], dtype="S1"))[indexer.tuple]
andersy005 marked this conversation as resolved.
Show resolved Hide resolved
array(b'abc', dtype='|S3')
"""

Expand All @@ -240,7 +241,7 @@ def __init__(self, array):

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

@property
def shape(self) -> tuple[int, ...]:
Expand All @@ -249,15 +250,17 @@ def shape(self) -> tuple[int, ...]:
def __repr__(self):
return f"{type(self).__name__}({self.array!r})"

def _vindex_get(self, key):
def _vindex_get(self, key: _IndexerKey):
return _numpy_char_to_bytes(self.array.vindex[key])

def _oindex_get(self, key):
def _oindex_get(self, key: _IndexerKey):
return _numpy_char_to_bytes(self.array.oindex[key])

def __getitem__(self, key):
def __getitem__(self, key: _IndexerKey):
from xarray.core.indexing import CompatIndexedTuple

# require slicing the last dimension completely
key = type(key)(indexing.expanded_indexer(key.tuple, self.array.ndim))
if key.tuple[-1] != slice(None):
indexer = indexing.expanded_indexer(key, self.array.ndim)
if indexer[-1] != slice(None):
raise IndexError("too many indices")
return _numpy_char_to_bytes(self.array[key])
return _numpy_char_to_bytes(self.array[CompatIndexedTuple(indexer, "basic")])
andersy005 marked this conversation as resolved.
Show resolved Hide resolved
4 changes: 2 additions & 2 deletions xarray/coding/variables.py
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ class NativeEndiannessArray(indexing.ExplicitlyIndexedNDArrayMixin):
dtype('int16')

>>> indexer = indexing.BasicIndexer((slice(None),))
>>> NativeEndiannessArray(x)[indexer].dtype
>>> NativeEndiannessArray(x)[indexer.tuple].dtype
andersy005 marked this conversation as resolved.
Show resolved Hide resolved
dtype('int16')
"""

Expand Down Expand Up @@ -138,7 +138,7 @@ class BoolTypeArray(indexing.ExplicitlyIndexedNDArrayMixin):
dtype('bool')

>>> indexer = indexing.BasicIndexer((slice(None),))
>>> BoolTypeArray(x)[indexer].dtype
>>> BoolTypeArray(x)[indexer.tuple].dtype
andersy005 marked this conversation as resolved.
Show resolved Hide resolved
dtype('bool')
"""

Expand Down
Loading
Loading