From 60b77c23d53b91371435af9c4724ab90f1ba78aa Mon Sep 17 00:00:00 2001 From: Benoit Bovy Date: Fri, 21 Feb 2025 11:03:18 +0100 Subject: [PATCH 1/2] Index.isel: more permissive return type This allows an index to return a new index of another type, e.g., a 1-dimensional CoordinateTransformIndex to return a PandasIndex when a new transform cannot be computed (selection at arbitrary locations). --- xarray/core/indexes.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/xarray/core/indexes.py b/xarray/core/indexes.py index 43e231e84d4..8d0ce8e922a 100644 --- a/xarray/core/indexes.py +++ b/xarray/core/indexes.py @@ -209,7 +209,7 @@ def to_pandas_index(self) -> pd.Index: def isel( self, indexers: Mapping[Any, int | slice | np.ndarray | Variable] - ) -> Self | None: + ) -> Index | None: """Maybe returns a new index from the current index itself indexed by positional indexers. @@ -1424,7 +1424,7 @@ def create_variables( def isel( self, indexers: Mapping[Any, int | slice | np.ndarray | Variable] - ) -> Self | None: + ) -> Index | None: # TODO: support returning a new index (e.g., possible to re-calculate the # the transform or calculate another transform on a reduced dimension space) return None From 6fc8f26d446151a3fa89f4cf54632ed43c8e59f3 Mon Sep 17 00:00:00 2001 From: Benoit Bovy Date: Fri, 21 Feb 2025 11:05:42 +0100 Subject: [PATCH 2/2] Index.equals: more permissive `other` type. Xarray alignment logic is such that Xarray indexes are always compared with other indexes of the same type. However, this is not necessarily the case for "meta" indexes (i.e., indexes encapsulating one or more index objects that may have another type) that are dispatching `equals` to their wrapped indexes. --- xarray/core/indexes.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/xarray/core/indexes.py b/xarray/core/indexes.py index 8d0ce8e922a..22febf0b707 100644 --- a/xarray/core/indexes.py +++ b/xarray/core/indexes.py @@ -304,7 +304,7 @@ def reindex_like(self, other: Self) -> dict[Hashable, Any]: """ raise NotImplementedError(f"{self!r} doesn't support re-indexing labels") - def equals(self, other: Self) -> bool: + def equals(self, other: Index) -> bool: """Compare this index with another index of the same type. Implementation is optional but required in order to support alignment. @@ -1486,7 +1486,9 @@ def sel( return IndexSelResult(results) - def equals(self, other: Self) -> bool: + def equals(self, other: Index) -> bool: + if not isinstance(other, CoordinateTransformIndex): + return False return self.transform.equals(other.transform) def rename(