Skip to content

Commit 42add9f

Browse files
committed
BUG: series.loc[[]] with non-unique MultiInex pandas-dev#13691
1 parent 388ec49 commit 42add9f

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
@@ -401,6 +401,7 @@ Indexing
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`)
403403
- Bug in :meth:`DataFrame.__getitem__` and :meth:`DataFrame.loc.__getitem__` with :class:`IntervalIndex` columns and a numeric indexer (:issue:`26490`)
404+
- Bug in :meth:`Series.loc.__getitem__` with a non-unique :class:`MultiIndex` and an empty-list indexer (:issue:`13691`)
404405

405406
Missing
406407
^^^^^^^

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)