Skip to content

Commit 2b86a6c

Browse files
authored
backwards compat for equals (#326)
* backwards-compat for indexes without `exclude` * comments and a marker about the deprecation
1 parent 2dab9ae commit 2b86a6c

File tree

1 file changed

+23
-4
lines changed

1 file changed

+23
-4
lines changed

pint_xarray/index.py

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import inspect
2+
13
from xarray import Variable
24
from xarray.core.indexes import Index, PandasIndex
35

@@ -80,14 +82,31 @@ def equals(self, other, *, exclude=None):
8082
if not isinstance(other, PintIndex):
8183
return False
8284

83-
# for now we require exactly matching units to avoid the potentially expensive conversion
85+
# for now we require exactly matching units to avoid the potentially
86+
# expensive conversion
8487
if self.units != other.units:
8588
return False
8689

87-
# Explicitly pass `exclude`, the index does not officially support
88-
# indexes other than the PandasIndex
90+
# TODO:
91+
# - remove try-except once we can drop xarray<2025.06.0
92+
# - remove compat once we can require a version of xarray that completed
93+
# the deprecation cycle
94+
try:
95+
from xarray.core.indexes import _wrap_index_equals
96+
97+
equals = _wrap_index_equals(self.index)
98+
kwargs = {"exclude": exclude}
99+
except ImportError: # pragma: no cover
100+
equals = self.index.equals
101+
signature = inspect.signature(self.index.equals)
102+
103+
if "exclude" in signature.parameters:
104+
kwargs = {"exclude": exclude}
105+
else:
106+
kwargs = {}
107+
89108
# Last to avoid the potentially expensive comparison
90-
return self.index.equals(other.index, exclude=exclude)
109+
return equals(other.index, **kwargs)
91110

92111
def roll(self, shifts):
93112
return self._replace(self.index.roll(shifts))

0 commit comments

Comments
 (0)