diff --git a/pandas/core/generic.py b/pandas/core/generic.py index 8708de68c0860..6424589843d76 100644 --- a/pandas/core/generic.py +++ b/pandas/core/generic.py @@ -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 diff --git a/pandas/tests/frame/methods/test_drop.py b/pandas/tests/frame/methods/test_drop.py index d9668ce46c943..48c5d3a2e982b 100644 --- a/pandas/tests/frame/methods/test_drop.py +++ b/pandas/tests/frame/methods/test_drop.py @@ -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( [