Skip to content
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

BUG: Series.drop() with MultiIndex: inconsistent behaviour #30377

Merged
merged 2 commits into from
Dec 26, 2019
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
2 changes: 1 addition & 1 deletion doc/source/whatsnew/v1.0.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -789,7 +789,7 @@ MultiIndex
^^^^^^^^^^

- Constructior for :class:`MultiIndex` verifies that the given ``sortorder`` is compatible with the actual ``lexsort_depth`` if ``verify_integrity`` parameter is ``True`` (the default) (:issue:`28735`)
-
- Series and MultiIndex `.drop` with `MultiIndex` raise exception if labels not in given in level (:issue:`8594`)
-

I/O
Expand Down
6 changes: 4 additions & 2 deletions pandas/core/indexes/multi.py
Original file line number Diff line number Diff line change
Expand Up @@ -2065,7 +2065,7 @@ def drop(self, codes, level=None, errors="raise"):
dropped : MultiIndex
"""
if level is not None:
return self._drop_from_level(codes, level)
return self._drop_from_level(codes, level, errors)

if not isinstance(codes, (np.ndarray, Index)):
try:
Expand Down Expand Up @@ -2103,13 +2103,15 @@ def drop(self, codes, level=None, errors="raise"):

return self.delete(inds)

def _drop_from_level(self, codes, level):
def _drop_from_level(self, codes, level, errors="raise"):
codes = com.index_labels_to_array(codes)
i = self._get_level_number(level)
index = self.levels[i]
values = index.get_indexer(codes)

mask = ~algos.isin(self.codes[i], values)
if mask.all() and errors != "ignore":
raise KeyError(f"labels {codes} not found in level")

return self[mask]

Expand Down
33 changes: 33 additions & 0 deletions pandas/tests/indexes/multi/test_drop.py
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,39 @@ def test_drop_not_lexsorted():
tm.assert_index_equal(lexsorted_mi.drop("a"), not_lexsorted_mi.drop("a"))


@pytest.mark.parametrize(
"msg,labels,level",
[
(r"labels \[4\] not found in level", 4, "a"),
(r"labels \[7\] not found in level", 7, "b"),
],
)
def test_drop_raise_exception_if_labels_not_in_level(msg, labels, level):
# GH 8594
mi = MultiIndex.from_arrays([[1, 2, 3], [4, 5, 6]], names=["a", "b"])
s = pd.Series([10, 20, 30], index=mi)
df = pd.DataFrame([10, 20, 30], index=mi)

with pytest.raises(KeyError, match=msg):
s.drop(labels, level=level)
with pytest.raises(KeyError, match=msg):
df.drop(labels, level=level)


@pytest.mark.parametrize("labels,level", [(4, "a"), (7, "b")])
def test_drop_errors_ignore(labels, level):
# GH 8594
mi = MultiIndex.from_arrays([[1, 2, 3], [4, 5, 6]], names=["a", "b"])
s = pd.Series([10, 20, 30], index=mi)
df = pd.DataFrame([10, 20, 30], index=mi)

expected_s = s.drop(labels, level=level, errors="ignore")
tm.assert_series_equal(s, expected_s)

expected_df = df.drop(labels, level=level, errors="ignore")
tm.assert_frame_equal(df, expected_df)


def test_drop_with_non_unique_datetime_index_and_invalid_keys():
# GH 30399

Expand Down