Skip to content

REGR: MultiIndex Indexing #35353

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 3 commits into from
Jul 21, 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
5 changes: 4 additions & 1 deletion pandas/core/indexes/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -3397,7 +3397,10 @@ def _reindex_non_unique(self, target):
new_indexer = np.arange(len(self.take(indexer)))
new_indexer[~check] = -1

new_index = Index(new_labels, name=self.name)
if isinstance(self, ABCMultiIndex):
new_index = type(self).from_tuples(new_labels, names=self.names)
else:
new_index = Index(new_labels, name=self.name)
return new_index, indexer, new_indexer

# --------------------------------------------------------------------
Expand Down
19 changes: 19 additions & 0 deletions pandas/tests/indexing/multiindex/test_loc.py
Original file line number Diff line number Diff line change
Expand Up @@ -491,3 +491,22 @@ def test_loc_datetime_mask_slicing():
),
)
tm.assert_series_equal(result, expected)


def test_loc_with_mi_indexer():
# https://github.com/pandas-dev/pandas/issues/35351
df = DataFrame(
data=[["a", 1], ["a", 0], ["b", 1], ["c", 2]],
index=MultiIndex.from_tuples(
[(0, 1), (1, 0), (1, 1), (1, 1)], names=["index", "date"]
),
columns=["author", "price"],
)
idx = MultiIndex.from_tuples([(0, 1), (1, 1)], names=["index", "date"])
result = df.loc[idx, :]
expected = DataFrame(
[["a", 1], ["b", 1], ["c", 2]],
index=MultiIndex.from_tuples([(0, 1), (1, 1), (1, 1)], names=["index", "date"]),
columns=["author", "price"],
)
tm.assert_frame_equal(result, expected)