Skip to content

Commit

Permalink
REGR: MultiIndex Indexing (#35353)
Browse files Browse the repository at this point in the history
* REGR: MultiIndex Indexing

* add test
  • Loading branch information
simonjayhawkins committed Jul 21, 2020
1 parent e673b69 commit 3e88e17
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 1 deletion.
5 changes: 4 additions & 1 deletion pandas/core/indexes/base.py
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
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)

0 comments on commit 3e88e17

Please sign in to comment.