Skip to content

Commit a8738ba

Browse files
KalyanGokhalejreback
authored andcommitted
CLN: Comparison methods for MultiIndex should have consistent behaviour for all nlevels (GH21149) (pandas-dev#21195)
1 parent fd121ed commit a8738ba

File tree

3 files changed

+20
-1
lines changed

3 files changed

+20
-1
lines changed

doc/source/whatsnew/v0.23.2.txt

+1
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ Bug Fixes
5252
**Indexing**
5353

5454
- Bug in :meth:`Index.get_indexer_non_unique` with categorical key (:issue:`21448`)
55+
- Bug in comparison operations for :class:`MultiIndex` where error was raised on equality / inequality comparison involving a MultiIndex with ``nlevels == 1`` (:issue:`21149`)
5556
-
5657

5758
**I/O**

pandas/core/indexes/base.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,8 @@ def cmp_method(self, other):
9191
if needs_i8_conversion(self) and needs_i8_conversion(other):
9292
return self._evaluate_compare(other, op)
9393

94-
if is_object_dtype(self) and self.nlevels == 1:
94+
from .multi import MultiIndex
95+
if is_object_dtype(self) and not isinstance(self, MultiIndex):
9596
# don't pass MultiIndex
9697
with np.errstate(all='ignore'):
9798
result = ops._comp_method_OBJECT_ARRAY(op, self.values, other)

pandas/tests/indexes/test_multi.py

+17
Original file line numberDiff line numberDiff line change
@@ -3307,3 +3307,20 @@ def test_duplicate_multiindex_labels(self):
33073307
with pytest.raises(ValueError):
33083308
ind.set_levels([['A', 'B', 'A', 'A', 'B'], [2, 1, 3, -2, 5]],
33093309
inplace=True)
3310+
3311+
def test_multiindex_compare(self):
3312+
# GH 21149
3313+
# Ensure comparison operations for MultiIndex with nlevels == 1
3314+
# behave consistently with those for MultiIndex with nlevels > 1
3315+
3316+
midx = pd.MultiIndex.from_product([[0, 1]])
3317+
3318+
# Equality self-test: MultiIndex object vs self
3319+
expected = pd.Series([True, True])
3320+
result = pd.Series(midx == midx)
3321+
tm.assert_series_equal(result, expected)
3322+
3323+
# Greater than comparison: MultiIndex object vs self
3324+
expected = pd.Series([False, False])
3325+
result = pd.Series(midx > midx)
3326+
tm.assert_series_equal(result, expected)

0 commit comments

Comments
 (0)