diff --git a/doc/source/whatsnew/v3.0.0.rst b/doc/source/whatsnew/v3.0.0.rst index c865509dea172..35f66f7aebd17 100644 --- a/doc/source/whatsnew/v3.0.0.rst +++ b/doc/source/whatsnew/v3.0.0.rst @@ -742,6 +742,7 @@ Other Deprecations - Deprecated silent casting of non-datetime 'other' to datetime in :meth:`Series.combine_first` (:issue:`62931`) - 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 support for the Dataframe Interchange Protocol (:issue:`56732`) +- Deprecated the "columns" keyword in :meth:`Series.drop` as it was silently ignored (:issue:`39509`) - Deprecated the 'inplace' keyword from :meth:`Resampler.interpolate`, as passing ``True`` raises ``AttributeError`` (:issue:`58690`) .. --------------------------------------------------------------------------- diff --git a/pandas/core/series.py b/pandas/core/series.py index 1a8645cf1815d..5bcba0b1b09b8 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, @@ -5509,6 +5510,7 @@ def drop( errors: IgnoreRaise = ..., ) -> Series | None: ... + @deprecate_kwarg(Pandas4Warning, "columns", new_arg_name=None) # GH#39509 def drop( self, labels: IndexLabel | ListLike = None, @@ -5537,7 +5539,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 e6f84941f6b1a..61483879d0552 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"])