Skip to content

Commit 6a6a1ba

Browse files
khemkaran10Khemkaran
andauthored
BUG: If both index and axis are passed to DataFrame.drop, raise a clear error (pandas-dev#61855)
Co-authored-by: Khemkaran <sunny@Khemkaran-MacBook-Pro.local>
1 parent 4b18266 commit 6a6a1ba

File tree

2 files changed

+14
-0
lines changed

2 files changed

+14
-0
lines changed

pandas/core/generic.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4569,6 +4569,8 @@ def drop(
45694569
axis_name = self._get_axis_name(axis)
45704570
axes = {axis_name: labels}
45714571
elif index is not None or columns is not None:
4572+
if axis == 1:
4573+
raise ValueError("Cannot specify both 'axis' and 'index'/'columns'")
45724574
axes = {"index": index}
45734575
if self.ndim == 2:
45744576
axes["columns"] = columns

pandas/tests/frame/methods/test_drop.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -346,6 +346,18 @@ def test_drop_multiindex_other_level_nan(self):
346346
)
347347
tm.assert_frame_equal(result, expected)
348348

349+
def test_drop_raise_with_both_axis_and_index(self):
350+
# GH#61823
351+
df = DataFrame(
352+
[[1, 2, 3], [3, 4, 5], [5, 6, 7]],
353+
index=["a", "b", "c"],
354+
columns=["d", "e", "f"],
355+
)
356+
357+
msg = "Cannot specify both 'axis' and 'index'/'columns'"
358+
with pytest.raises(ValueError, match=msg):
359+
df.drop(index="b", axis=1)
360+
349361
def test_drop_nonunique(self):
350362
df = DataFrame(
351363
[

0 commit comments

Comments
 (0)