Skip to content

Commit df33a9a

Browse files
authored
More permissive Index typing (#10067)
* 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). * 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.
1 parent cf0d0ea commit df33a9a

File tree

1 file changed

+6
-4
lines changed

1 file changed

+6
-4
lines changed

xarray/core/indexes.py

+6-4
Original file line numberDiff line numberDiff line change
@@ -209,7 +209,7 @@ def to_pandas_index(self) -> pd.Index:
209209

210210
def isel(
211211
self, indexers: Mapping[Any, int | slice | np.ndarray | Variable]
212-
) -> Self | None:
212+
) -> Index | None:
213213
"""Maybe returns a new index from the current index itself indexed by
214214
positional indexers.
215215
@@ -304,7 +304,7 @@ def reindex_like(self, other: Self) -> dict[Hashable, Any]:
304304
"""
305305
raise NotImplementedError(f"{self!r} doesn't support re-indexing labels")
306306

307-
def equals(self, other: Self) -> bool:
307+
def equals(self, other: Index) -> bool:
308308
"""Compare this index with another index of the same type.
309309
310310
Implementation is optional but required in order to support alignment.
@@ -1424,7 +1424,7 @@ def create_variables(
14241424

14251425
def isel(
14261426
self, indexers: Mapping[Any, int | slice | np.ndarray | Variable]
1427-
) -> Self | None:
1427+
) -> Index | None:
14281428
# TODO: support returning a new index (e.g., possible to re-calculate the
14291429
# the transform or calculate another transform on a reduced dimension space)
14301430
return None
@@ -1486,7 +1486,9 @@ def sel(
14861486

14871487
return IndexSelResult(results)
14881488

1489-
def equals(self, other: Self) -> bool:
1489+
def equals(self, other: Index) -> bool:
1490+
if not isinstance(other, CoordinateTransformIndex):
1491+
return False
14901492
return self.transform.equals(other.transform)
14911493

14921494
def rename(

0 commit comments

Comments
 (0)