From 17346f7b63e498769711abcd1e16e7c75287fe7f Mon Sep 17 00:00:00 2001 From: Illviljan <14371165+Illviljan@users.noreply.github.com> Date: Sat, 21 Sep 2024 19:55:59 +0200 Subject: [PATCH] Update indexing.py --- xarray/core/indexing.py | 27 ++++++++++++--------------- 1 file changed, 12 insertions(+), 15 deletions(-) diff --git a/xarray/core/indexing.py b/xarray/core/indexing.py index 55b179a6b7e..d3a6d340d90 100644 --- a/xarray/core/indexing.py +++ b/xarray/core/indexing.py @@ -505,9 +505,7 @@ class ExplicitlyIndexed: __slots__ = () - def __array__( - self, dtype: np.typing.DTypeLike = None, /, *, copy: bool | None = None - ) -> np.ndarray: + def __array__(self, dtype: np.typing.DTypeLike = None) -> np.ndarray: # Leave casting to an array up to the underlying array type. return np.asarray(self.get_duck_array(), dtype=dtype, copy=copy) @@ -522,6 +520,11 @@ def get_duck_array(self): key = BasicIndexer((slice(None),) * self.ndim) return self[key] + def __array__(self, dtype: np.typing.DTypeLike = None) -> np.ndarray: + # This is necessary because we apply the indexing key in self.get_duck_array() + # Note this is the base class for all lazy indexing classes + return np.asarray(self.get_duck_array(), dtype=dtype) + def _oindex_get(self, indexer: OuterIndexer): raise NotImplementedError( f"{self.__class__.__name__}._oindex_get method should be overridden" @@ -567,10 +570,8 @@ def __init__(self, array, indexer_cls: type[ExplicitIndexer] = BasicIndexer): self.array = as_indexable(array) self.indexer_cls = indexer_cls - def __array__( - self, dtype: np.typing.DTypeLike = None, /, *, copy: bool | None = None - ) -> np.ndarray: - return np.asarray(self.get_duck_array(), dtype=dtype, copy=copy) + def __array__(self, dtype: np.typing.DTypeLike = None) -> np.ndarray: + return np.asarray(self.get_duck_array(), dtype=dtype) def get_duck_array(self): return self.array.get_duck_array() @@ -829,8 +830,8 @@ def __init__(self, array): def _ensure_cached(self): self.array = as_indexable(self.array.get_duck_array()) - # def __array__(self, dtype: np.typing.DTypeLike = None) -> np.ndarray: - # return np.asarray(self.get_duck_array(), dtype=dtype) + def __array__(self, dtype: np.typing.DTypeLike = None) -> np.ndarray: + return np.asarray(self.get_duck_array(), dtype=dtype) def get_duck_array(self): self._ensure_cached() @@ -1673,9 +1674,7 @@ def __init__(self, array: pd.Index, dtype: DTypeLike = None): def dtype(self) -> np.dtype: return self._dtype - def __array__( - self, dtype: np.typing.DTypeLike = None, /, *, copy: bool | None = None - ) -> np.ndarray: + def __array__(self, dtype: DTypeLike = None) -> np.ndarray: if dtype is None: dtype = self.dtype array = self.array @@ -1832,9 +1831,7 @@ def __init__( super().__init__(array, dtype) self.level = level - def __array__( - self, dtype: np.typing.DTypeLike = None, /, *, copy: bool | None = None - ) -> np.ndarray: + def __array__(self, dtype: DTypeLike = None) -> np.ndarray: if dtype is None: dtype = self.dtype if self.level is not None: