From d8ca3372d8f8119bbb2fd5ec7b6529f8aa3ff48d Mon Sep 17 00:00:00 2001 From: Brock Date: Sun, 4 Oct 2020 16:32:23 -0700 Subject: [PATCH] REF: IntervalIndex.equals defer to IntervalArray.equals --- 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 5105b5b9cc57b..555d6e32c7b9d 100644 --- a/pandas/core/arrays/interval.py +++ b/pandas/core/arrays/interval.py @@ -696,6 +696,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 8855d987af745..fc77876af41f1 100644 --- a/pandas/core/indexes/interval.py +++ b/pandas/core/indexes/interval.py @@ -983,19 +983,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