Skip to content

REGR: drop raising with ea index and duplicates #45983

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 4 commits into from
Feb 16, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
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.4.2.rst
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ including other versions of pandas.

Fixed regressions
~~~~~~~~~~~~~~~~~
-
- Fixed regression in :meth:`DataFrame.drop` and :meth:`Series.drop` when :class:`Index` had extension dtype and duplicates (:issue:`45820`)
-

.. ---------------------------------------------------------------------------
Expand Down
3 changes: 3 additions & 0 deletions pandas/core/generic.py
Original file line number Diff line number Diff line change
Expand Up @@ -4342,6 +4342,9 @@ def _drop_axis(
if errors == "raise" and labels_missing:
raise KeyError(f"{labels} not found in axis")

if is_extension_array_dtype(mask.dtype):
mask = mask.to_numpy()
Copy link
Member

Choose a reason for hiding this comment

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

if mask is BooleanArray, then we need to specify dtype=bool otherwise we get object dtype here. or could implement EA.nonzero.

comment with GH ref?

Copy link
Member Author

Choose a reason for hiding this comment

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

Thx added the dtype and comment. I must have removed the dtype somehow after fixing this


Copy link
Contributor

Choose a reason for hiding this comment

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

i guess we maybe want to define .nonzero() on EA arrays

Copy link
Member

Choose a reason for hiding this comment

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

+1

Copy link
Member Author

Choose a reason for hiding this comment

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

opened #46025

indexer = mask.nonzero()[0]
new_axis = axis.take(indexer)

Expand Down
12 changes: 12 additions & 0 deletions pandas/tests/frame/methods/test_drop.py
Original file line number Diff line number Diff line change
Expand Up @@ -537,3 +537,15 @@ def test_drop_level_missing_label_multiindex(self):
df = DataFrame(index=MultiIndex.from_product([range(3), range(3)]))
with pytest.raises(KeyError, match="labels \\[5\\] not found in level"):
df.drop(5, level=0)

@pytest.mark.parametrize("idx, level", [(["a", "b"], 0), (["a"], None)])
def test_drop_index_ea_dtype(self, any_numeric_ea_dtype, idx, level):
# GH#45860
df = DataFrame(
{"a": [1, 2, 2], "b": 100}, dtype=any_numeric_ea_dtype
Copy link
Member

Choose a reason for hiding this comment

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

case with a pd.NA?

Copy link
Member Author

Choose a reason for hiding this comment

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

Added one more element, but this should not matter in this case I think

Copy link
Member

Choose a reason for hiding this comment

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

the case i had in mind is where the BooleanArray mask that this PR casts to ndarray contains a pd.NA. Is that not reachable?

Copy link
Member Author

Choose a reason for hiding this comment

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

Ah got you. I don't think so no. Having NA on both sides evaluates to False. Added the case

).set_index(idx)
result = df.drop(Index([2]), level=level)
expected = DataFrame(
{"a": [1], "b": 100}, dtype=any_numeric_ea_dtype
).set_index(idx)
tm.assert_frame_equal(result, expected)
14 changes: 13 additions & 1 deletion pandas/tests/series/methods/test_drop.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
import pytest

from pandas import Series
from pandas import (
Index,
Series,
)
import pandas._testing as tm


Expand Down Expand Up @@ -98,3 +101,12 @@ def test_drop_pos_args_deprecation():
result = ser.drop(1, 0)
expected = Series([1, 3], index=[0, 2])
tm.assert_series_equal(result, expected)


def test_drop_index_ea_dtype(any_numeric_ea_dtype):
# GH#45860
df = Series(100, index=Index([1, 2, 2], dtype=any_numeric_ea_dtype))
idx = Index([df.index[1]])
result = df.drop(idx)
expected = Series(100, index=Index([1], dtype=any_numeric_ea_dtype))
tm.assert_series_equal(result, expected)