From 70884793230ecdbbdb9cd8d1f230b260d71ff8b7 Mon Sep 17 00:00:00 2001 From: Khemkaran Date: Mon, 14 Jul 2025 22:29:20 +0530 Subject: [PATCH 1/5] if both index and axis are passed to drop() it will raise a better error message --- pandas/core/generic.py | 2 ++ pandas/tests/frame/methods/test_drop.py | 7 +++++++ 2 files changed, 9 insertions(+) diff --git a/pandas/core/generic.py b/pandas/core/generic.py index 8708de68c0860..aa679f6f3dc19 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 and index is not None: + raise ValueError("Cannot specify both 'axis' and 'index'") 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..3cf1abd229e2a 100644 --- a/pandas/tests/frame/methods/test_drop.py +++ b/pandas/tests/frame/methods/test_drop.py @@ -346,6 +346,13 @@ def test_drop_multiindex_other_level_nan(self): ) tm.assert_frame_equal(result, expected) + def test_drop_multiindex_with_axis_and_index(self): + df = DataFrame({'a': [1, 2, 3], 'b': ['foo', 'foo', 'bar']}) + df = pd.concat([df], keys=['foo'], axis=1) + msg = "Cannot specify both 'axis' and 'index'" + with pytest.raises(ValueError, match=msg): + df.drop(index='b', level=1, axis=1) + def test_drop_nonunique(self): df = DataFrame( [ From 8bfa3ebf11a5b1107f84a335ad2b3a2ae5079e75 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Mon, 14 Jul 2025 18:04:03 +0000 Subject: [PATCH 2/5] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- pandas/tests/frame/methods/test_drop.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/pandas/tests/frame/methods/test_drop.py b/pandas/tests/frame/methods/test_drop.py index 3cf1abd229e2a..5361e8f882b79 100644 --- a/pandas/tests/frame/methods/test_drop.py +++ b/pandas/tests/frame/methods/test_drop.py @@ -347,11 +347,11 @@ def test_drop_multiindex_other_level_nan(self): tm.assert_frame_equal(result, expected) def test_drop_multiindex_with_axis_and_index(self): - df = DataFrame({'a': [1, 2, 3], 'b': ['foo', 'foo', 'bar']}) - df = pd.concat([df], keys=['foo'], axis=1) + df = DataFrame({"a": [1, 2, 3], "b": ["foo", "foo", "bar"]}) + df = pd.concat([df], keys=["foo"], axis=1) msg = "Cannot specify both 'axis' and 'index'" with pytest.raises(ValueError, match=msg): - df.drop(index='b', level=1, axis=1) + df.drop(index="b", level=1, axis=1) def test_drop_nonunique(self): df = DataFrame( From 022c6f58ce08b23a570bdceaca36f8806db645a7 Mon Sep 17 00:00:00 2001 From: Khemkaran Date: Tue, 15 Jul 2025 11:37:14 +0530 Subject: [PATCH 3/5] removed index check from the raise condition, axis==1 will suffice --- pandas/core/generic.py | 4 ++-- pandas/tests/frame/methods/test_drop.py | 7 +++---- 2 files changed, 5 insertions(+), 6 deletions(-) diff --git a/pandas/core/generic.py b/pandas/core/generic.py index aa679f6f3dc19..6424589843d76 100644 --- a/pandas/core/generic.py +++ b/pandas/core/generic.py @@ -4569,8 +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 and index is not None: - raise ValueError("Cannot specify both 'axis' and 'index'") + 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 5361e8f882b79..eac0f20353e88 100644 --- a/pandas/tests/frame/methods/test_drop.py +++ b/pandas/tests/frame/methods/test_drop.py @@ -346,12 +346,11 @@ def test_drop_multiindex_other_level_nan(self): ) tm.assert_frame_equal(result, expected) - def test_drop_multiindex_with_axis_and_index(self): + def test_drop_with_both_axis_and_index(self): df = DataFrame({"a": [1, 2, 3], "b": ["foo", "foo", "bar"]}) - df = pd.concat([df], keys=["foo"], axis=1) - msg = "Cannot specify both 'axis' and 'index'" + msg = "Cannot specify both 'axis' and 'index'/'columns'" with pytest.raises(ValueError, match=msg): - df.drop(index="b", level=1, axis=1) + df.drop(index="b", axis=1) def test_drop_nonunique(self): df = DataFrame( From b94f6d3f1e9e71a12ec1a6cadca70b6907fe6212 Mon Sep 17 00:00:00 2001 From: Khemkaran Date: Tue, 15 Jul 2025 11:57:51 +0530 Subject: [PATCH 4/5] test name and test df change in test_drop.py --- pandas/tests/frame/methods/test_drop.py | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/pandas/tests/frame/methods/test_drop.py b/pandas/tests/frame/methods/test_drop.py index eac0f20353e88..bbe1379b79667 100644 --- a/pandas/tests/frame/methods/test_drop.py +++ b/pandas/tests/frame/methods/test_drop.py @@ -346,8 +346,13 @@ def test_drop_multiindex_other_level_nan(self): ) tm.assert_frame_equal(result, expected) - def test_drop_with_both_axis_and_index(self): - df = DataFrame({"a": [1, 2, 3], "b": ["foo", "foo", "bar"]}) + 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) From d5521891c0b2dc66c781ffd51912f222156cc1ff Mon Sep 17 00:00:00 2001 From: Khemkaran Sevta <168984037+khemkaran10@users.noreply.github.com> Date: Tue, 15 Jul 2025 23:18:38 +0530 Subject: [PATCH 5/5] added comment --- pandas/tests/frame/methods/test_drop.py | 1 + 1 file changed, 1 insertion(+) diff --git a/pandas/tests/frame/methods/test_drop.py b/pandas/tests/frame/methods/test_drop.py index bbe1379b79667..48c5d3a2e982b 100644 --- a/pandas/tests/frame/methods/test_drop.py +++ b/pandas/tests/frame/methods/test_drop.py @@ -347,6 +347,7 @@ 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"],