Skip to content

Commit

Permalink
BUG: Series.loc[[]] with non-unique MultiIndex pandas-dev#13691
Browse files Browse the repository at this point in the history
  • Loading branch information
jbrockmendel committed Oct 16, 2020
1 parent 53b58e8 commit 2d2b1ed
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 0 deletions.
1 change: 1 addition & 0 deletions doc/source/whatsnew/v1.2.0.rst
Expand Up @@ -400,6 +400,7 @@ Indexing
- Bug in :meth:`DataFrame.sort_index` where parameter ascending passed as a list on a single level index gives wrong result. (:issue:`32334`)
- 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`)
- Bug in indexing with boolean masks on datetime-like values sometimes returning a view instead of a copy (:issue:`36210`)
- Bug in :meth:`Series.loc.__getitem__` with a non-unique :class:`MultiIndex` and an empty-list indexer (:issue:`13691`)

Missing
^^^^^^^
Expand Down
4 changes: 4 additions & 0 deletions pandas/core/indexes/base.py
Expand Up @@ -3404,6 +3404,10 @@ def _reindex_non_unique(self, target):
"""
target = ensure_index(target)
if len(target) == 0:
# GH#13691
return self[:0], np.array([], dtype=np.intp), None

indexer, missing = self.get_indexer_non_unique(target)
check = indexer != -1
new_labels = self.take(indexer[check])
Expand Down
14 changes: 14 additions & 0 deletions pandas/tests/series/indexing/test_multiindex.py
Expand Up @@ -49,3 +49,17 @@ def test_nat_multi_index(ix_data, exp_data):

expected = pd.DataFrame(exp_data)
tm.assert_frame_equal(result, expected)


def test_loc_getitem_multiindex_nonunique_len_zero():
# GH#13691
mi = pd.MultiIndex.from_product([[0], [1, 1]])
ser = pd.Series(0, index=mi)

res = ser.loc[[]]

expected = ser[:0]
tm.assert_series_equal(res, expected)

res2 = ser.loc[ser.iloc[0:0]]
tm.assert_series_equal(res2, expected)

0 comments on commit 2d2b1ed

Please sign in to comment.