|
| 1 | +import inspect |
| 2 | + |
1 | 3 | from xarray import Variable |
2 | 4 | from xarray.core.indexes import Index, PandasIndex |
3 | 5 |
|
@@ -80,14 +82,31 @@ def equals(self, other, *, exclude=None): |
80 | 82 | if not isinstance(other, PintIndex): |
81 | 83 | return False |
82 | 84 |
|
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 |
84 | 87 | if self.units != other.units: |
85 | 88 | return False |
86 | 89 |
|
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 | + |
89 | 108 | # Last to avoid the potentially expensive comparison |
90 | | - return self.index.equals(other.index, exclude=exclude) |
| 109 | + return equals(other.index, **kwargs) |
91 | 110 |
|
92 | 111 | def roll(self, shifts): |
93 | 112 | return self._replace(self.index.roll(shifts)) |
|
0 commit comments