diff --git a/doc/source/whatsnew/v1.3.0.rst b/doc/source/whatsnew/v1.3.0.rst index 24cd41383e9d7..10a042373a8d1 100644 --- a/doc/source/whatsnew/v1.3.0.rst +++ b/doc/source/whatsnew/v1.3.0.rst @@ -693,6 +693,7 @@ Deprecations - Deprecated passing arguments as positional in :meth:`DataFrame.reset_index` (other than ``"level"``) and :meth:`Series.reset_index` (:issue:`41485`) - Deprecated construction of :class:`Series` or :class:`DataFrame` with ``DatetimeTZDtype`` data and ``datetime64[ns]`` dtype. Use ``Series(data).dt.tz_localize(None)`` instead (:issue:`41555`,:issue:`33401`) - Deprecated passing arguments as positional in :meth:`DataFrame.where` and :meth:`Series.where` (other than ``"cond"`` and ``"other"``) (:issue:`41485`) +- Deprecated passing arguments as positional in :meth:`DataFrame.drop` (other than ``"labels"``) and :meth:`Series.drop` (:issue:`41485`) - .. _whatsnew_130.deprecations.nuisance_columns: diff --git a/pandas/core/frame.py b/pandas/core/frame.py index 75d2f4c591053..710aee6581dc2 100644 --- a/pandas/core/frame.py +++ b/pandas/core/frame.py @@ -4766,6 +4766,7 @@ def reindex(self, *args, **kwargs) -> DataFrame: kwargs.pop("labels", None) return super().reindex(**kwargs) + @deprecate_nonkeyword_arguments(version=None, allowed_args=["self", "labels"]) def drop( self, labels=None, diff --git a/pandas/core/series.py b/pandas/core/series.py index 686e6966d8e24..88580f0427a34 100644 --- a/pandas/core/series.py +++ b/pandas/core/series.py @@ -4522,6 +4522,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=None, 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 523e5209f3762..76e24a27e0854 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) @@ -482,6 +482,18 @@ def test_drop_with_duplicate_columns2(self): result = df2.drop("C", axis=1) 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"In a future version of pandas all arguments of DataFrame\.drop " + r"except for the argument 'labels' will be keyword-only" + ) + with tm.assert_produces_warning(FutureWarning, match=msg): + result = df.drop("a", 1) + expected = DataFrame(index=[0, 1, 2]) + tm.assert_frame_equal(result, expected) + def test_drop_inplace_no_leftover_column_reference(self): # GH 13934 df = DataFrame({"a": [1, 2, 3]}) diff --git a/pandas/tests/groupby/transform/test_transform.py b/pandas/tests/groupby/transform/test_transform.py index 1949d03998512..b5092f83e1a9f 100644 --- a/pandas/tests/groupby/transform/test_transform.py +++ b/pandas/tests/groupby/transform/test_transform.py @@ -901,7 +901,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)() diff --git a/pandas/tests/series/methods/test_drop.py b/pandas/tests/series/methods/test_drop.py index 7ded8ac902d78..a566f8f62d72e 100644 --- a/pandas/tests/series/methods/test_drop.py +++ b/pandas/tests/series/methods/test_drop.py @@ -84,3 +84,16 @@ 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"In a future version of pandas all arguments of Series\.drop " + r"except for the argument 'labels' will be keyword-only" + ) + with tm.assert_produces_warning(FutureWarning, match=msg): + result = ser.drop(1, 0) + expected = Series([1, 3], index=[0, 2]) + tm.assert_series_equal(result, expected)