Skip to content

Commit

Permalink
REF: IntervalIndex.equals defer to IntervalArray.equals (pandas-dev#3…
Browse files Browse the repository at this point in the history
  • Loading branch information
jbrockmendel authored Oct 6, 2020
1 parent ebd9906 commit c5637a8
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 11 deletions.
9 changes: 9 additions & 0 deletions pandas/core/arrays/_mixins.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@
from pandas.util._decorators import cache_readonly, doc
from pandas.util._validators import validate_fillna_kwargs

from pandas.core.dtypes.common import is_dtype_equal
from pandas.core.dtypes.inference import is_array_like
from pandas.core.dtypes.missing import array_equivalent

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

# ------------------------------------------------------------------------

def equals(self, other) -> bool:
if type(self) is not type(other):
return False
if not is_dtype_equal(self.dtype, other.dtype):
return False
return bool(array_equivalent(self._ndarray, other._ndarray))

def _values_for_argsort(self):
return self._ndarray

Expand Down
10 changes: 10 additions & 0 deletions pandas/core/arrays/interval.py
Original file line number Diff line number Diff line change
Expand Up @@ -706,6 +706,16 @@ def astype(self, dtype, copy=True):
msg = f"Cannot cast {type(self).__name__} to dtype {dtype}"
raise TypeError(msg) from err

def equals(self, other) -> bool:
if type(self) != type(other):
return False

return bool(
self.closed == other.closed
and self.left.equals(other.left)
and self.right.equals(other.right)
)

@classmethod
def _concat_same_type(cls, to_concat):
"""
Expand Down
13 changes: 2 additions & 11 deletions pandas/core/indexes/interval.py
Original file line number Diff line number Diff line change
Expand Up @@ -997,19 +997,10 @@ def equals(self, other: object) -> bool:
if self.is_(other):
return True

# if we can coerce to an IntervalIndex then we can compare
if not isinstance(other, IntervalIndex):
if not is_interval_dtype(other):
return False
other = Index(other)
if not isinstance(other, IntervalIndex):
return False
return False

return (
self.left.equals(other.left)
and self.right.equals(other.right)
and self.closed == other.closed
)
return self._data.equals(other._data)

# --------------------------------------------------------------------
# Set Operations
Expand Down

0 comments on commit c5637a8

Please sign in to comment.