Skip to content

Commit 4c395b0

Browse files
authored
BUG: Series.loc[[]] with non-unique MultiIndex #13691 (#37161)
1 parent 710148e commit 4c395b0

File tree

3 files changed

+19
-0
lines changed

3 files changed

+19
-0
lines changed

doc/source/whatsnew/v1.2.0.rst

+1
Original file line numberDiff line numberDiff line change
@@ -400,6 +400,7 @@ Indexing
400400
- Bug in :meth:`DataFrame.sort_index` where parameter ascending passed as a list on a single level index gives wrong result. (:issue:`32334`)
401401
- Bug in :meth:`DataFrame.reset_index` was incorrectly raising a ``ValueError`` for input with a :class:`MultiIndex` with missing values in a level with ``Categorical`` dtype (:issue:`24206`)
402402
- Bug in indexing with boolean masks on datetime-like values sometimes returning a view instead of a copy (:issue:`36210`)
403+
- Bug in :meth:`Series.loc.__getitem__` with a non-unique :class:`MultiIndex` and an empty-list indexer (:issue:`13691`)
403404

404405
Missing
405406
^^^^^^^

pandas/core/indexes/base.py

+4
Original file line numberDiff line numberDiff line change
@@ -3404,6 +3404,10 @@ def _reindex_non_unique(self, target):
34043404
34053405
"""
34063406
target = ensure_index(target)
3407+
if len(target) == 0:
3408+
# GH#13691
3409+
return self[:0], np.array([], dtype=np.intp), None
3410+
34073411
indexer, missing = self.get_indexer_non_unique(target)
34083412
check = indexer != -1
34093413
new_labels = self.take(indexer[check])

pandas/tests/series/indexing/test_multiindex.py

+14
Original file line numberDiff line numberDiff line change
@@ -49,3 +49,17 @@ def test_nat_multi_index(ix_data, exp_data):
4949

5050
expected = pd.DataFrame(exp_data)
5151
tm.assert_frame_equal(result, expected)
52+
53+
54+
def test_loc_getitem_multiindex_nonunique_len_zero():
55+
# GH#13691
56+
mi = pd.MultiIndex.from_product([[0], [1, 1]])
57+
ser = pd.Series(0, index=mi)
58+
59+
res = ser.loc[[]]
60+
61+
expected = ser[:0]
62+
tm.assert_series_equal(res, expected)
63+
64+
res2 = ser.loc[ser.iloc[0:0]]
65+
tm.assert_series_equal(res2, expected)

0 commit comments

Comments
 (0)