Skip to content

Commit 9522071

Browse files
committed
Merged
2 parents 4abc49a + 9dc59f0 commit 9522071

File tree

2 files changed

+14
-3
lines changed

2 files changed

+14
-3
lines changed

doc/source/whatsnew/v0.22.0.txt

+2-1
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,11 @@ Other Enhancements
2828

2929
.. _whatsnew_0220.api_breaking:
3030

31+
3132
Backwards incompatible API changes
3233
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
3334

34-
-
35+
- When created with duplicate labels, ``MultiIndex`` now raises a ``ValueError``. (:issue:`17464`)
3536
-
3637
-
3738

pandas/tests/indexes/test_multi.py

+12-2
Original file line numberDiff line numberDiff line change
@@ -1573,7 +1573,6 @@ def test_is_(self):
15731573
# shouldn't change
15741574
assert mi2.is_(mi)
15751575
mi4 = mi3.view()
1576-
# GH 17464 - Remove duplicate MultiIndex levels
15771576
mi4.set_levels([lrange(10), lrange(10)], inplace=True)
15781577
assert not mi4.is_(mi3)
15791578
mi5 = mi.view()
@@ -2376,7 +2375,6 @@ def test_level_setting_resets_attributes(self):
23762375
['A', 'A', 'B', 'B', 'B'], [1, 2, 1, 2, 3]
23772376
])
23782377
assert ind.is_monotonic
2379-
# GH 17464 - Remove duplicate MultiIndex levels
23802378
ind.set_levels([['A', 'B'], [1, 3, 2]], inplace=True)
23812379
# if this fails, probably didn't reset the cache correctly.
23822380
assert not ind.is_monotonic
@@ -2980,3 +2978,15 @@ def test_nan_stays_float(self):
29802978
assert pd.isna(df0.index.get_level_values(1)).all()
29812979
# the following failed in 0.14.1
29822980
assert pd.isna(dfm.index.get_level_values(1)[:-1]).all()
2981+
2982+
def test_duplicate_multiindex_labels(self):
2983+
# GH 17464
2984+
# Make sure that a MultiIndex with duplicate levels throws a ValueError
2985+
with pytest.raises(ValueError):
2986+
ind = pd.MultiIndex([['A'] * 10, range(10)], [[0] * 10, range(10)])
2987+
# And that using set_levels with duplicate levels fails
2988+
ind = MultiIndex.from_arrays([['A', 'A', 'B', 'B', 'B'],
2989+
[1, 2, 1, 2, 3]])
2990+
with pytest.raises(ValueError):
2991+
ind.set_levels([['A', 'B', 'A', 'A', 'B'], [2, 1, 3, -2, 5]],
2992+
inplace=True)

0 commit comments

Comments
 (0)