From 05579520af1e4e4365a7da227df9fe6ce654e060 Mon Sep 17 00:00:00 2001 From: Brock Date: Mon, 13 Oct 2025 16:02:35 -0700 Subject: [PATCH 1/2] DEPR: columns keyword in Series.drop --- doc/source/whatsnew/v3.0.0.rst | 1 + pandas/core/series.py | 6 +++++- pandas/core/window/rolling.py | 10 ++++++---- pandas/tests/series/methods/test_drop.py | 11 +++++++++++ 4 files changed, 23 insertions(+), 5 deletions(-) diff --git a/doc/source/whatsnew/v3.0.0.rst b/doc/source/whatsnew/v3.0.0.rst index 448ceffdaa1eb..0dd43ab0032d3 100644 --- a/doc/source/whatsnew/v3.0.0.rst +++ b/doc/source/whatsnew/v3.0.0.rst @@ -717,6 +717,7 @@ Other Deprecations - Deprecated allowing ``fill_value`` that cannot be held in the original dtype (excepting NA values for integer and bool dtypes) in :meth:`Series.unstack` and :meth:`DataFrame.unstack` (:issue:`12189`, :issue:`53868`) - Deprecated allowing ``fill_value`` that cannot be held in the original dtype (excepting NA values for integer and bool dtypes) in :meth:`Series.shift` and :meth:`DataFrame.shift` (:issue:`53802`) - Deprecated slicing on a :class:`Series` or :class:`DataFrame` with a :class:`DatetimeIndex` using a ``datetime.date`` object, explicitly cast to :class:`Timestamp` instead (:issue:`35830`) +- Deprecated the "columns" keyword in :meth:`Series.drop` as it was silently ignored (:issue:`39509`) .. --------------------------------------------------------------------------- .. _whatsnew_300.prior_deprecations: diff --git a/pandas/core/series.py b/pandas/core/series.py index a5c3bb8d51e8a..75e6152b6ee6e 100644 --- a/pandas/core/series.py +++ b/pandas/core/series.py @@ -53,6 +53,7 @@ from pandas.util._decorators import ( Appender, Substitution, + deprecate_kwarg, deprecate_nonkeyword_arguments, doc, set_module, @@ -5064,6 +5065,7 @@ def drop( errors: IgnoreRaise = ..., ) -> Series | None: ... + @deprecate_kwarg(Pandas4Warning, "columns", new_arg_name=None) # GH#39509 def drop( self, labels: IndexLabel | ListLike = None, @@ -5092,7 +5094,9 @@ def drop( Redundant for application on Series, but 'index' can be used instead of 'labels'. columns : single label or list-like - No change is made to the Series; use 'index' or 'labels' instead. + Not supported; use 'index' or 'labels' instead. + + .. deprecated: 3.0.0 level : int or level name, optional For MultiIndex, level for which the labels will be removed. inplace : bool, default False diff --git a/pandas/core/window/rolling.py b/pandas/core/window/rolling.py index d3c417a008916..8985c763208a9 100644 --- a/pandas/core/window/rolling.py +++ b/pandas/core/window/rolling.py @@ -680,7 +680,8 @@ def __init__( # GH 32262: It's convention to keep the grouping column in # groupby., but unexpected to users in # groupby.rolling. - obj = obj.drop(columns=self._grouper.names, errors="ignore") + if obj.ndim == 2: + obj = obj.drop(columns=self._grouper.names, errors="ignore") # GH 15354 if kwargs.get("step") is not None: raise NotImplementedError("step not implemented for groupby") @@ -715,7 +716,7 @@ def _apply( if key not in self.obj.index.names or key is None ] - if len(drop_columns) != len(groupby_keys): + if len(drop_columns) != len(groupby_keys) and result.ndim == 2: # Our result will have still kept the column in the result result = result.drop(columns=drop_columns, errors="ignore") @@ -758,8 +759,9 @@ def _apply_pairwise( """ Apply the given pairwise function given 2 pandas objects (DataFrame/Series) """ - # Manually drop the grouping column first - target = target.drop(columns=self._grouper.names, errors="ignore") + if target.ndim == 2: + # Manually drop the grouping column first + target = target.drop(columns=self._grouper.names, errors="ignore") result = super()._apply_pairwise(target, other, pairwise, func, numeric_only) # 1) Determine the levels + codes of the groupby levels if other is not None and not all( diff --git a/pandas/tests/series/methods/test_drop.py b/pandas/tests/series/methods/test_drop.py index d2a5a3324e886..5ed5dbe066c73 100644 --- a/pandas/tests/series/methods/test_drop.py +++ b/pandas/tests/series/methods/test_drop.py @@ -1,5 +1,7 @@ import pytest +from pandas.errors import Pandas4Warning + from pandas import ( Index, Series, @@ -97,3 +99,12 @@ def test_drop_index_ea_dtype(any_numeric_ea_dtype): result = df.drop(idx) expected = Series(100, index=Index([1], dtype=any_numeric_ea_dtype)) tm.assert_series_equal(result, expected) + + +def test_drop_series_columns_deprecated(): + # GH#39509 + ser = Series({"a": 1, "b": 3}) + + msg = "the 'columns' keyword is deprecated and will be removed" + with tm.assert_produces_warning(Pandas4Warning, match=msg): + ser.drop(columns=["a"]) From 73c10188841f5bb2dcf8a3cfe321dd3eeaeabaae Mon Sep 17 00:00:00 2001 From: Brock Date: Tue, 14 Oct 2025 11:20:46 -0700 Subject: [PATCH 2/2] missing colon --- pandas/core/series.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pandas/core/series.py b/pandas/core/series.py index 75e6152b6ee6e..a5b3ac616111f 100644 --- a/pandas/core/series.py +++ b/pandas/core/series.py @@ -5096,7 +5096,7 @@ def drop( columns : single label or list-like Not supported; use 'index' or 'labels' instead. - .. deprecated: 3.0.0 + .. deprecated:: 3.0.0 level : int or level name, optional For MultiIndex, level for which the labels will be removed. inplace : bool, default False