Skip to content

Commit 7088479

Browse files
KhemkaranKhemkaran
authored andcommitted
if both index and axis are passed to drop() it will raise a better error message
1 parent bc6ad14 commit 7088479

File tree

2 files changed

+9
-0
lines changed

2 files changed

+9
-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 and index is not None:
4573+
raise ValueError("Cannot specify both 'axis' and 'index'")
45724574
axes = {"index": index}
45734575
if self.ndim == 2:
45744576
axes["columns"] = columns

pandas/tests/frame/methods/test_drop.py

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

349+
def test_drop_multiindex_with_axis_and_index(self):
350+
df = DataFrame({'a': [1, 2, 3], 'b': ['foo', 'foo', 'bar']})
351+
df = pd.concat([df], keys=['foo'], axis=1)
352+
msg = "Cannot specify both 'axis' and 'index'"
353+
with pytest.raises(ValueError, match=msg):
354+
df.drop(index='b', level=1, axis=1)
355+
349356
def test_drop_nonunique(self):
350357
df = DataFrame(
351358
[

0 commit comments

Comments
 (0)