From c5637a877d08c156d2c5193927b5e181a9b52d5f Mon Sep 17 00:00:00 2001 From: jbrockmendel Date: Tue, 6 Oct 2020 15:52:57 -0700 Subject: [PATCH] REF: IntervalIndex.equals defer to IntervalArray.equals (#36871) --- pandas/core/arrays/_mixins.py | 9 +++++++++ pandas/core/arrays/interval.py | 10 ++++++++++ pandas/core/indexes/interval.py | 13 ++----------- 3 files changed, 21 insertions(+), 11 deletions(-) diff --git a/pandas/core/arrays/_mixins.py b/pandas/core/arrays/_mixins.py index 4d13a18c8ef0b..95a003efbe1d0 100644 --- a/pandas/core/arrays/_mixins.py +++ b/pandas/core/arrays/_mixins.py @@ -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 @@ -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 diff --git a/pandas/core/arrays/interval.py b/pandas/core/arrays/interval.py index 94c6c5aed9c0d..a06e0c74ec03b 100644 --- a/pandas/core/arrays/interval.py +++ b/pandas/core/arrays/interval.py @@ -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): """ diff --git a/pandas/core/indexes/interval.py b/pandas/core/indexes/interval.py index 4a877621a94c2..e7747761d2a29 100644 --- a/pandas/core/indexes/interval.py +++ b/pandas/core/indexes/interval.py @@ -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