Skip to content

Commit 08935ba

Browse files
committed
Add IndexingAdapter mixin
1 parent a43af86 commit 08935ba

File tree

1 file changed

+21
-5
lines changed

1 file changed

+21
-5
lines changed

xarray/core/indexing.py

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -516,6 +516,20 @@ def get_duck_array(self):
516516
return self.array
517517

518518

519+
class IndexingAdapter:
520+
"""Marker class for indexing adapters.
521+
522+
These classes translate between Xarray's indexing semantics and the underlying array's
523+
indexing semantics.
524+
"""
525+
526+
async def async_get_duck_array():
527+
"""These classes are applied to in-memory arrays, so async get isn't needed."""
528+
raise RuntimeError(
529+
f"{type(self).__name__} async_get_duck_array was called. This should not happen."
530+
)
531+
532+
519533
class ExplicitlyIndexedNDArrayMixin(NDArrayMixin, ExplicitlyIndexed):
520534
__slots__ = ()
521535

@@ -1589,7 +1603,7 @@ def is_fancy_indexer(indexer: Any) -> bool:
15891603
return True
15901604

15911605

1592-
class NumpyIndexingAdapter(ExplicitlyIndexedNDArrayMixin):
1606+
class NumpyIndexingAdapter(ExplicitlyIndexedNDArrayMixin, IndexingAdapter):
15931607
"""Wrap a NumPy array to use explicit indexing."""
15941608

15951609
__slots__ = ("array",)
@@ -1668,7 +1682,7 @@ def __init__(self, array):
16681682
self.array = array
16691683

16701684

1671-
class ArrayApiIndexingAdapter(ExplicitlyIndexedNDArrayMixin):
1685+
class ArrayApiIndexingAdapter(ExplicitlyIndexedNDArrayMixin, IndexingAdapter):
16721686
"""Wrap an array API array to use explicit indexing."""
16731687

16741688
__slots__ = ("array",)
@@ -1733,7 +1747,7 @@ def _assert_not_chunked_indexer(idxr: tuple[Any, ...]) -> None:
17331747
)
17341748

17351749

1736-
class DaskIndexingAdapter(ExplicitlyIndexedNDArrayMixin):
1750+
class DaskIndexingAdapter(ExplicitlyIndexedNDArrayMixin, IndexingAdapter):
17371751
"""Wrap a dask array to support explicit indexing."""
17381752

17391753
__slots__ = ("array",)
@@ -1809,7 +1823,7 @@ def transpose(self, order):
18091823
return self.array.transpose(order)
18101824

18111825

1812-
class PandasIndexingAdapter(ExplicitlyIndexedNDArrayMixin):
1826+
class PandasIndexingAdapter(ExplicitlyIndexedNDArrayMixin, IndexingAdapter):
18131827
"""Wrap a pandas.Index to preserve dtypes and handle explicit indexing."""
18141828

18151829
__slots__ = ("_dtype", "array")
@@ -2135,7 +2149,9 @@ def copy(self, deep: bool = True) -> Self:
21352149
return type(self)(array, self._dtype, self.level)
21362150

21372151

2138-
class CoordinateTransformIndexingAdapter(ExplicitlyIndexedNDArrayMixin):
2152+
class CoordinateTransformIndexingAdapter(
2153+
ExplicitlyIndexedNDArrayMixin, IndexingAdapter
2154+
):
21392155
"""Wrap a CoordinateTransform as a lazy coordinate array.
21402156
21412157
Supports explicit indexing (both outer and vectorized).

0 commit comments

Comments
 (0)