Skip to content

Commit 02f61da

Browse files
committed
Add IndexingAdapter mixin class
1 parent a61b161 commit 02f61da

File tree

1 file changed

+19
-7
lines changed

1 file changed

+19
-7
lines changed

xarray/core/indexing.py

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -520,8 +520,7 @@ class ExplicitlyIndexedNDArrayMixin(NDArrayMixin, ExplicitlyIndexed):
520520
__slots__ = ()
521521

522522
def get_duck_array(self):
523-
key = BasicIndexer((slice(None),) * self.ndim)
524-
return self[key]
523+
raise NotImplementedError
525524

526525
def _oindex_get(self, indexer: OuterIndexer):
527526
raise NotImplementedError(
@@ -559,6 +558,17 @@ def vindex(self) -> IndexCallable:
559558
return IndexCallable(self._vindex_get, self._vindex_set)
560559

561560

561+
class IndexingAdapter:
562+
"""Marker class for indexing adapters.
563+
These classes translate between Xarray's indexing semantics and the underlying array's
564+
indexing semantics.
565+
"""
566+
567+
def get_duck_array(self):
568+
key = BasicIndexer((slice(None),) * self.ndim)
569+
return self[key]
570+
571+
562572
class ImplicitToExplicitIndexingAdapter(NDArrayMixin):
563573
"""Wrap an array, converting tuples into the indicated explicit indexer."""
564574

@@ -1526,7 +1536,7 @@ def is_fancy_indexer(indexer: Any) -> bool:
15261536
return True
15271537

15281538

1529-
class NumpyIndexingAdapter(ExplicitlyIndexedNDArrayMixin):
1539+
class NumpyIndexingAdapter(IndexingAdapter, ExplicitlyIndexedNDArrayMixin):
15301540
"""Wrap a NumPy array to use explicit indexing."""
15311541

15321542
__slots__ = ("array",)
@@ -1605,7 +1615,7 @@ def __init__(self, array):
16051615
self.array = array
16061616

16071617

1608-
class ArrayApiIndexingAdapter(ExplicitlyIndexedNDArrayMixin):
1618+
class ArrayApiIndexingAdapter(IndexingAdapter, ExplicitlyIndexedNDArrayMixin):
16091619
"""Wrap an array API array to use explicit indexing."""
16101620

16111621
__slots__ = ("array",)
@@ -1670,7 +1680,7 @@ def _assert_not_chunked_indexer(idxr: tuple[Any, ...]) -> None:
16701680
)
16711681

16721682

1673-
class DaskIndexingAdapter(ExplicitlyIndexedNDArrayMixin):
1683+
class DaskIndexingAdapter(IndexingAdapter, ExplicitlyIndexedNDArrayMixin):
16741684
"""Wrap a dask array to support explicit indexing."""
16751685

16761686
__slots__ = ("array",)
@@ -1746,7 +1756,7 @@ def transpose(self, order):
17461756
return self.array.transpose(order)
17471757

17481758

1749-
class PandasIndexingAdapter(ExplicitlyIndexedNDArrayMixin):
1759+
class PandasIndexingAdapter(IndexingAdapter, ExplicitlyIndexedNDArrayMixin):
17501760
"""Wrap a pandas.Index to preserve dtypes and handle explicit indexing."""
17511761

17521762
__slots__ = ("_dtype", "array")
@@ -2063,7 +2073,9 @@ def copy(self, deep: bool = True) -> Self:
20632073
return type(self)(array, self._dtype, self.level)
20642074

20652075

2066-
class CoordinateTransformIndexingAdapter(ExplicitlyIndexedNDArrayMixin):
2076+
class CoordinateTransformIndexingAdapter(
2077+
IndexingAdapter, ExplicitlyIndexedNDArrayMixin
2078+
):
20672079
"""Wrap a CoordinateTransform as a lazy coordinate array.
20682080
20692081
Supports explicit indexing (both outer and vectorized).

0 commit comments

Comments
 (0)