From c24d4d9d975a711e0d783b392363e9cddca5ac24 Mon Sep 17 00:00:00 2001 From: Simon Hawkins Date: Fri, 17 Jul 2020 17:55:03 +0100 Subject: [PATCH 1/3] REF: remove special casing from Index.equals (always dispatch to subclass) --- pandas/core/indexes/base.py | 15 +++++---------- 1 file changed, 5 insertions(+), 10 deletions(-) diff --git a/pandas/core/indexes/base.py b/pandas/core/indexes/base.py index 3dbee7d0929cb..85f626b2b87c4 100644 --- a/pandas/core/indexes/base.py +++ b/pandas/core/indexes/base.py @@ -4249,16 +4249,11 @@ def equals(self, other: Any) -> bool: if not isinstance(other, Index): return False - if is_object_dtype(self.dtype) and not is_object_dtype(other.dtype): - # if other is not object, use other's logic for coercion - return other.equals(self) - - if isinstance(other, ABCMultiIndex): - # d-level MultiIndex can equal d-tuple Index - return other.equals(self) - - if is_extension_array_dtype(other.dtype): - # All EA-backed Index subclasses override equals + if ( + isinstance(other, type(self)) + and type(other) is not type(self) + and other.equals is not self.equals + ): return other.equals(self) return array_equivalent(self._values, other._values) From ee3576f6ed4e55315867b53d316e4a383af44d08 Mon Sep 17 00:00:00 2001 From: Simon Hawkins Date: Sat, 18 Jul 2020 09:00:25 +0100 Subject: [PATCH 2/3] add comment --- pandas/core/indexes/base.py | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/pandas/core/indexes/base.py b/pandas/core/indexes/base.py index 85f626b2b87c4..18e94886740d3 100644 --- a/pandas/core/indexes/base.py +++ b/pandas/core/indexes/base.py @@ -4249,6 +4249,10 @@ def equals(self, other: Any) -> bool: if not isinstance(other, Index): return False + # If other is a subclass of self and defines it's own equals method, we + # dispatch to the subclass method. For instance for a MultiIndex, + # a d-level MultiIndex can equal d-tuple Index. + # Note: All EA-backed Index subclasses override equals if ( isinstance(other, type(self)) and type(other) is not type(self) From 060cf580eb25e45cb187f5fbfaa3f5df549e76fb Mon Sep 17 00:00:00 2001 From: Simon Hawkins Date: Sat, 18 Jul 2020 09:33:38 +0100 Subject: [PATCH 3/3] empty commit