Skip to content

Commit 3e88e17

Browse files
REGR: MultiIndex Indexing (pandas-dev#35353)
* REGR: MultiIndex Indexing * add test
1 parent e673b69 commit 3e88e17

File tree

2 files changed

+23
-1
lines changed

2 files changed

+23
-1
lines changed

pandas/core/indexes/base.py

+4-1
Original file line numberDiff line numberDiff line change
@@ -3397,7 +3397,10 @@ def _reindex_non_unique(self, target):
33973397
new_indexer = np.arange(len(self.take(indexer)))
33983398
new_indexer[~check] = -1
33993399

3400-
new_index = Index(new_labels, name=self.name)
3400+
if isinstance(self, ABCMultiIndex):
3401+
new_index = type(self).from_tuples(new_labels, names=self.names)
3402+
else:
3403+
new_index = Index(new_labels, name=self.name)
34013404
return new_index, indexer, new_indexer
34023405

34033406
# --------------------------------------------------------------------

pandas/tests/indexing/multiindex/test_loc.py

+19
Original file line numberDiff line numberDiff line change
@@ -491,3 +491,22 @@ def test_loc_datetime_mask_slicing():
491491
),
492492
)
493493
tm.assert_series_equal(result, expected)
494+
495+
496+
def test_loc_with_mi_indexer():
497+
# https://github.com/pandas-dev/pandas/issues/35351
498+
df = DataFrame(
499+
data=[["a", 1], ["a", 0], ["b", 1], ["c", 2]],
500+
index=MultiIndex.from_tuples(
501+
[(0, 1), (1, 0), (1, 1), (1, 1)], names=["index", "date"]
502+
),
503+
columns=["author", "price"],
504+
)
505+
idx = MultiIndex.from_tuples([(0, 1), (1, 1)], names=["index", "date"])
506+
result = df.loc[idx, :]
507+
expected = DataFrame(
508+
[["a", 1], ["b", 1], ["c", 2]],
509+
index=MultiIndex.from_tuples([(0, 1), (1, 1), (1, 1)], names=["index", "date"]),
510+
columns=["author", "price"],
511+
)
512+
tm.assert_frame_equal(result, expected)

0 commit comments

Comments
 (0)