Skip to content

FIX BUG: If both index and axis are passed to drop(), it will raise ValueError with clear message. #61855

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

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
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: 2 additions & 0 deletions pandas/core/generic.py
Original file line number Diff line number Diff line change
Expand Up @@ -4569,6 +4569,8 @@ def drop(
axis_name = self._get_axis_name(axis)
axes = {axis_name: labels}
elif index is not None or columns is not None:
if axis == 1:
raise ValueError("Cannot specify both 'axis' and 'index'/'columns'")
axes = {"index": index}
if self.ndim == 2:
axes["columns"] = columns
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 @@ -346,6 +346,18 @@ def test_drop_multiindex_other_level_nan(self):
)
tm.assert_frame_equal(result, expected)

def test_drop_raise_with_both_axis_and_index(self):
# GH#61823
df = DataFrame(
[[1, 2, 3], [3, 4, 5], [5, 6, 7]],
index=["a", "b", "c"],
columns=["d", "e", "f"],
)

msg = "Cannot specify both 'axis' and 'index'/'columns'"
with pytest.raises(ValueError, match=msg):
df.drop(index="b", axis=1)

def test_drop_nonunique(self):
df = DataFrame(
[
Expand Down
Loading