From 955c51dac263338440a8cfce1ccadceb5fd642f2 Mon Sep 17 00:00:00 2001 From: phofl Date: Fri, 18 Feb 2022 12:24:51 +0100 Subject: [PATCH 1/2] BUG: Multiindex.equals not commutative for ea dtype --- doc/source/whatsnew/v1.5.0.rst | 2 +- pandas/core/indexes/multi.py | 4 ++++ pandas/tests/indexes/multi/test_equivalence.py | 8 ++++++++ 3 files changed, 13 insertions(+), 1 deletion(-) diff --git a/doc/source/whatsnew/v1.5.0.rst b/doc/source/whatsnew/v1.5.0.rst index c8b2617ffc535..fa3f0d0e6833c 100644 --- a/doc/source/whatsnew/v1.5.0.rst +++ b/doc/source/whatsnew/v1.5.0.rst @@ -347,7 +347,7 @@ Missing MultiIndex ^^^^^^^^^^ -- +- Bug in :class:`MultiIndex.equals` not commutative when only one side has extension array dtype (:issue:`46026`) - I/O diff --git a/pandas/core/indexes/multi.py b/pandas/core/indexes/multi.py index cc6c92a27e344..3a1f2251b2484 100644 --- a/pandas/core/indexes/multi.py +++ b/pandas/core/indexes/multi.py @@ -3611,6 +3611,10 @@ def equals(self, other: object) -> bool: # i.e. ExtensionArray if not self_values.equals(other_values): return False + elif not isinstance(other_values, np.ndarray): + # i.e. other is ExtensionArray + if not other_values.equals(self_values): + return False else: if not array_equivalent(self_values, other_values): return False diff --git a/pandas/tests/indexes/multi/test_equivalence.py b/pandas/tests/indexes/multi/test_equivalence.py index 3854aca9430a8..c6567b86f0da2 100644 --- a/pandas/tests/indexes/multi/test_equivalence.py +++ b/pandas/tests/indexes/multi/test_equivalence.py @@ -288,3 +288,11 @@ def test_multiindex_compare(): expected = Series([False, False]) result = Series(midx > midx) tm.assert_series_equal(result, expected) + + +def test_equals_ea_int_regular_int(): + # GH#46026 + mi1 = MultiIndex.from_arrays([Index([1, 2], dtype="Int64"), [3, 4]]) + mi2 = MultiIndex.from_arrays([[1, 2], [3, 4]]) + assert not mi1.equals(mi2) + assert not mi2.equals(mi1) From 5d06d630bbbae456322d3b5cb4bbcaeb964b2c9b Mon Sep 17 00:00:00 2001 From: phofl Date: Fri, 18 Feb 2022 13:18:59 +0100 Subject: [PATCH 2/2] Fix copy paste mistake --- pandas/core/indexes/multi.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/pandas/core/indexes/multi.py b/pandas/core/indexes/multi.py index 3a1f2251b2484..662f4d252e8cc 100644 --- a/pandas/core/indexes/multi.py +++ b/pandas/core/indexes/multi.py @@ -3611,10 +3611,10 @@ def equals(self, other: object) -> bool: # i.e. ExtensionArray if not self_values.equals(other_values): return False - elif not isinstance(other_values, np.ndarray): - # i.e. other is ExtensionArray - if not other_values.equals(self_values): - return False + elif not isinstance(other_values, np.ndarray): + # i.e. other is ExtensionArray + if not other_values.equals(self_values): + return False else: if not array_equivalent(self_values, other_values): return False