Skip to content

Series.loc with MultiIndex does not handle repeated keys properly #19414

Closed
@shoyer

Description

@shoyer

Code Sample, a copy-pastable example if possible

import pandas as pd
s = pd.Series([1, 2], pd.MultiIndex.from_arrays([['a', 'b']]))
print(s.loc[['a', 'a', 'b', 'b']])

Problem description

This prints the full series, not the series with repeated elements:

a    1
b    2
dtype: int64

Expected Output

a    1
a    1
b    2
b    2
dtype: int64

An easy work around is to use s.reindex(['a', 'a', 'b', 'b']) instead. But loc and reindex should return the same thing when all labels are found.

Output of pd.show_versions()

pandas 0.22.0

I think this behavior is present on earlier versions of pandas, too, but previously it was very difficult to construct a MultiIndex with only a single level, because they were automatically turned in a base Index.

Metadata

Metadata

Assignees

Labels

BugIndexingRelated to indexing on series/frames, not to indexes themselvesMultiIndexNeeds TestsUnit test(s) needed to prevent regressions

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions