Skip to content

BUG: Series.loc[[]] with non-unique MultiIndex #13691 #37161

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 16, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions doc/source/whatsnew/v1.2.0.rst
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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)