From e3f08cc2525da4caab5d210a99db8927828602ae Mon Sep 17 00:00:00 2001 From: Marco Gorelli Date: Sat, 15 May 2021 10:47:19 +0100 Subject: [PATCH 1/3] deprecate non-keyword arguments in drop --- doc/source/whatsnew/v1.3.0.rst | 1 + pandas/core/frame.py | 2 ++ pandas/core/series.py | 2 ++ pandas/io/stata.py | 4 +++- pandas/tests/frame/methods/test_drop.py | 17 +++++++++++++---- .../tests/groupby/transform/test_transform.py | 2 +- 6 files changed, 22 insertions(+), 6 deletions(-) diff --git a/doc/source/whatsnew/v1.3.0.rst b/doc/source/whatsnew/v1.3.0.rst index 622029adf357f..6bbcba863e6c5 100644 --- a/doc/source/whatsnew/v1.3.0.rst +++ b/doc/source/whatsnew/v1.3.0.rst @@ -647,6 +647,7 @@ Deprecations - Deprecated setting :attr:`Categorical._codes`, create a new :class:`Categorical` with the desired codes instead (:issue:`40606`) - Deprecated behavior of :meth:`DatetimeIndex.union` with mixed timezones; in a future version both will be cast to UTC instead of object dtype (:issue:`39328`) - Deprecated using ``usecols`` with out of bounds indices for ``read_csv`` with ``engine="c"`` (:issue:`25623`) +- Deprecated passing arguments (apart from ``labels``) as positional in :meth:`DataFrame.drop` and :meth:`Series.drop` (:issue:`41485`) .. --------------------------------------------------------------------------- diff --git a/pandas/core/frame.py b/pandas/core/frame.py index 2941b6ac01904..739adb782ea82 100644 --- a/pandas/core/frame.py +++ b/pandas/core/frame.py @@ -77,6 +77,7 @@ Appender, Substitution, deprecate_kwarg, + deprecate_nonkeyword_arguments, doc, rewrite_axis_style_signature, ) @@ -4738,6 +4739,7 @@ def reindex(self, *args, **kwargs) -> DataFrame: kwargs.pop("labels", None) return super().reindex(**kwargs) + @deprecate_nonkeyword_arguments(version="2.0", allowed_args=["self", "labels"]) def drop( self, labels=None, diff --git a/pandas/core/series.py b/pandas/core/series.py index c8e9898f9462a..9be95474b2828 100644 --- a/pandas/core/series.py +++ b/pandas/core/series.py @@ -51,6 +51,7 @@ from pandas.util._decorators import ( Appender, Substitution, + deprecate_nonkeyword_arguments, doc, ) from pandas.util._validators import ( @@ -4485,6 +4486,7 @@ def set_axis(self, labels, axis: Axis = 0, inplace: bool = False): def reindex(self, index=None, **kwargs): return super().reindex(index=index, **kwargs) + @deprecate_nonkeyword_arguments(version="2.0", allowed_args=["self", "labels"]) def drop( self, labels=None, diff --git a/pandas/io/stata.py b/pandas/io/stata.py index f1747f94a7ea8..e4f3bcb89cf7e 100644 --- a/pandas/io/stata.py +++ b/pandas/io/stata.py @@ -1761,7 +1761,9 @@ def _do_convert_missing(self, data: DataFrame, convert_missing: bool) -> DataFra if replacements: columns = data.columns replacement_df = DataFrame(replacements) - replaced = concat([data.drop(replacement_df.columns, 1), replacement_df], 1) + replaced = concat( + [data.drop(replacement_df.columns, axis=1), replacement_df], 1 + ) data = replaced[columns] return data diff --git a/pandas/tests/frame/methods/test_drop.py b/pandas/tests/frame/methods/test_drop.py index 5d2aabd372fd1..31ac0d137faf6 100644 --- a/pandas/tests/frame/methods/test_drop.py +++ b/pandas/tests/frame/methods/test_drop.py @@ -89,7 +89,7 @@ def test_drop_names(self): with pytest.raises(KeyError, match=msg): df.drop(["g"]) with pytest.raises(KeyError, match=msg): - df.drop(["g"], 1) + df.drop(["g"], axis=1) # errors = 'ignore' dropped = df.drop(["g"], errors="ignore") @@ -123,11 +123,11 @@ def test_drop(self): with pytest.raises(KeyError, match=r"\[5\] not found in axis"): simple.drop(5) with pytest.raises(KeyError, match=r"\['C'\] not found in axis"): - simple.drop("C", 1) + simple.drop("C", axis=1) with pytest.raises(KeyError, match=r"\[5\] not found in axis"): simple.drop([1, 5]) with pytest.raises(KeyError, match=r"\['C'\] not found in axis"): - simple.drop(["A", "C"], 1) + simple.drop(["A", "C"], axis=1) # errors = 'ignore' tm.assert_frame_equal(simple.drop(5, errors="ignore"), simple) @@ -201,7 +201,7 @@ def test_drop_api_equivalence(self): res2 = df.drop(index="a") tm.assert_frame_equal(res1, res2) - res1 = df.drop("d", 1) + res1 = df.drop("d", axis=1) res2 = df.drop(columns="d") tm.assert_frame_equal(res1, res2) @@ -481,3 +481,12 @@ def test_drop_with_duplicate_columns2(self): df2 = df.take([2, 0, 1, 2, 1], axis=1) result = df2.drop("C", axis=1) tm.assert_frame_equal(result, expected) + + def test_drop_pos_args_deprecation(self): + df = DataFrame({"a": [1, 2, 3]}) + msg = ( + r"Starting with Pandas version 2\.0 all arguments of drop except for the " + r"arguments 'self' and 'labels' will be keyword-only" + ) + with tm.assert_produces_warning(FutureWarning, match=msg): + df.drop("a", 1) diff --git a/pandas/tests/groupby/transform/test_transform.py b/pandas/tests/groupby/transform/test_transform.py index 09317cbeec658..4b2e1d03fc629 100644 --- a/pandas/tests/groupby/transform/test_transform.py +++ b/pandas/tests/groupby/transform/test_transform.py @@ -883,7 +883,7 @@ def test_pad_stable_sorting(fill_method): y = y[::-1] df = DataFrame({"x": x, "y": y}) - expected = df.drop("x", 1) + expected = df.drop("x", axis=1) result = getattr(df.groupby("x"), fill_method)() From 7912d31e1da4733e1dd6ebc6688610b735980012 Mon Sep 17 00:00:00 2001 From: Marco Gorelli Date: Sat, 15 May 2021 12:09:27 +0100 Subject: [PATCH 2/3] noop From 685a566502c6a56f0df32976c59ccbeecc69b5b2 Mon Sep 17 00:00:00 2001 From: Marco Gorelli Date: Sat, 15 May 2021 19:20:31 +0100 Subject: [PATCH 3/3] add series test --- pandas/tests/frame/methods/test_drop.py | 1 + pandas/tests/series/methods/test_drop.py | 11 +++++++++++ 2 files changed, 12 insertions(+) diff --git a/pandas/tests/frame/methods/test_drop.py b/pandas/tests/frame/methods/test_drop.py index 31ac0d137faf6..ef41151c84f44 100644 --- a/pandas/tests/frame/methods/test_drop.py +++ b/pandas/tests/frame/methods/test_drop.py @@ -483,6 +483,7 @@ def test_drop_with_duplicate_columns2(self): tm.assert_frame_equal(result, expected) def test_drop_pos_args_deprecation(self): + # https://github.com/pandas-dev/pandas/issues/41485 df = DataFrame({"a": [1, 2, 3]}) msg = ( r"Starting with Pandas version 2\.0 all arguments of drop except for the " diff --git a/pandas/tests/series/methods/test_drop.py b/pandas/tests/series/methods/test_drop.py index 7ded8ac902d78..19ea65f35c73f 100644 --- a/pandas/tests/series/methods/test_drop.py +++ b/pandas/tests/series/methods/test_drop.py @@ -84,3 +84,14 @@ def test_drop_non_empty_list(data, index, drop_labels): ser = Series(data=data, index=index, dtype=dtype) with pytest.raises(KeyError, match="not found in axis"): ser.drop(drop_labels) + + +def test_drop_pos_args_deprecation(): + # https://github.com/pandas-dev/pandas/issues/41485 + ser = Series([1, 2, 3]) + msg = ( + r"Starting with Pandas version 2\.0 all arguments of drop except for the " + r"arguments 'self' and 'labels' will be keyword-only" + ) + with tm.assert_produces_warning(FutureWarning, match=msg): + ser.drop(1, 0)