Skip to content

Commit c5637a8

Browse files
authored
REF: IntervalIndex.equals defer to IntervalArray.equals (#36871)
1 parent ebd9906 commit c5637a8

File tree

3 files changed

+21
-11
lines changed

3 files changed

+21
-11
lines changed

Diff for: pandas/core/arrays/_mixins.py

+9
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,9 @@
88
from pandas.util._decorators import cache_readonly, doc
99
from pandas.util._validators import validate_fillna_kwargs
1010

11+
from pandas.core.dtypes.common import is_dtype_equal
1112
from pandas.core.dtypes.inference import is_array_like
13+
from pandas.core.dtypes.missing import array_equivalent
1214

1315
from pandas.core import missing
1416
from pandas.core.algorithms import take, unique
@@ -115,6 +117,13 @@ def T(self: _T) -> _T:
115117

116118
# ------------------------------------------------------------------------
117119

120+
def equals(self, other) -> bool:
121+
if type(self) is not type(other):
122+
return False
123+
if not is_dtype_equal(self.dtype, other.dtype):
124+
return False
125+
return bool(array_equivalent(self._ndarray, other._ndarray))
126+
118127
def _values_for_argsort(self):
119128
return self._ndarray
120129

Diff for: pandas/core/arrays/interval.py

+10
Original file line numberDiff line numberDiff line change
@@ -706,6 +706,16 @@ def astype(self, dtype, copy=True):
706706
msg = f"Cannot cast {type(self).__name__} to dtype {dtype}"
707707
raise TypeError(msg) from err
708708

709+
def equals(self, other) -> bool:
710+
if type(self) != type(other):
711+
return False
712+
713+
return bool(
714+
self.closed == other.closed
715+
and self.left.equals(other.left)
716+
and self.right.equals(other.right)
717+
)
718+
709719
@classmethod
710720
def _concat_same_type(cls, to_concat):
711721
"""

Diff for: pandas/core/indexes/interval.py

+2-11
Original file line numberDiff line numberDiff line change
@@ -997,19 +997,10 @@ def equals(self, other: object) -> bool:
997997
if self.is_(other):
998998
return True
999999

1000-
# if we can coerce to an IntervalIndex then we can compare
10011000
if not isinstance(other, IntervalIndex):
1002-
if not is_interval_dtype(other):
1003-
return False
1004-
other = Index(other)
1005-
if not isinstance(other, IntervalIndex):
1006-
return False
1001+
return False
10071002

1008-
return (
1009-
self.left.equals(other.left)
1010-
and self.right.equals(other.right)
1011-
and self.closed == other.closed
1012-
)
1003+
return self._data.equals(other._data)
10131004

10141005
# --------------------------------------------------------------------
10151006
# Set Operations

0 commit comments

Comments
 (0)