Skip to content
Merged
Show file tree
Hide file tree
Changes from 4 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
11 changes: 11 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,17 @@ def test_drop_multiindex_other_level_nan(self):
)
tm.assert_frame_equal(result, expected)

def test_drop_raise_with_both_axis_and_index(self):
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