Skip to content

BUG: DataFrame.drop raising TypeError for string label with non-unique MultiIndex and no level provided #38220

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 9 commits into from
Dec 14, 2020
Merged
Show file tree
Hide file tree
Changes from 6 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.3.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ Missing
MultiIndex
^^^^^^^^^^

-
- Bug in :meth:`DataFrame.drop` raising ``TypeError`` when :class:`MultiIndex` is non-unique and no level is provided (:issue:`36293`)
-

I/O
Expand Down
8 changes: 8 additions & 0 deletions pandas/core/generic.py
Original file line number Diff line number Diff line change
Expand Up @@ -4173,6 +4173,14 @@ def _drop_axis(

# Case for non-unique axis
else:
if (
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should this case only be an issue starting on L4193? e.g. level is None there

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yep, this is correct. Question is what to do when level is None.

I think ~axis.get_level_values(level).isin(labels) is the way to go, so I check before reaching the if else block if level is None to set it to 0 in this case

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i think setting to level = 0 is fine, i would just move the check inside the block on L4193 as its not going to be true where it is if level is not None in the first place.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This would work as an alternative

isinstance(axis, MultiIndex)
and level is None
and isinstance(labels, str)
):
# Set level to zero in case of MultiIndex and label is string, because
# isin can't handle strings for MultiIndexes GH#36293
level = 0
labels = ensure_object(com.index_labels_to_array(labels))
if level is not None:
if not isinstance(axis, MultiIndex):
Expand Down
8 changes: 8 additions & 0 deletions pandas/tests/frame/methods/test_drop.py
Original file line number Diff line number Diff line change
Expand Up @@ -441,3 +441,11 @@ def test_inplace_drop_and_operation(self, operation, inplace):
# Perform operation and check result
getattr(y, operation)(1)
tm.assert_frame_equal(df, expected)

def test_drop_with_non_unique_multiindex(self):
# GH#36293
mi = MultiIndex.from_arrays([["x", "y", "x"], ["i", "j", "i"]])
df = DataFrame([1, 2, 3], index=mi)
result = df.drop(index="x")
expected = DataFrame([2], index=MultiIndex.from_arrays([["y"], ["j"]]))
tm.assert_frame_equal(result, expected)