Skip to content

Commit 0557952

Browse files
committed
DEPR: columns keyword in Series.drop
1 parent 8d32d57 commit 0557952

File tree

4 files changed

+23
-5
lines changed

4 files changed

+23
-5
lines changed

doc/source/whatsnew/v3.0.0.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -717,6 +717,7 @@ Other Deprecations
717717
- 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`)
718718
- 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`)
719719
- 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`)
720+
- Deprecated the "columns" keyword in :meth:`Series.drop` as it was silently ignored (:issue:`39509`)
720721

721722
.. ---------------------------------------------------------------------------
722723
.. _whatsnew_300.prior_deprecations:

pandas/core/series.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@
5353
from pandas.util._decorators import (
5454
Appender,
5555
Substitution,
56+
deprecate_kwarg,
5657
deprecate_nonkeyword_arguments,
5758
doc,
5859
set_module,
@@ -5064,6 +5065,7 @@ def drop(
50645065
errors: IgnoreRaise = ...,
50655066
) -> Series | None: ...
50665067

5068+
@deprecate_kwarg(Pandas4Warning, "columns", new_arg_name=None) # GH#39509
50675069
def drop(
50685070
self,
50695071
labels: IndexLabel | ListLike = None,
@@ -5092,7 +5094,9 @@ def drop(
50925094
Redundant for application on Series, but 'index' can be used instead
50935095
of 'labels'.
50945096
columns : single label or list-like
5095-
No change is made to the Series; use 'index' or 'labels' instead.
5097+
Not supported; use 'index' or 'labels' instead.
5098+
5099+
.. deprecated: 3.0.0
50965100
level : int or level name, optional
50975101
For MultiIndex, level for which the labels will be removed.
50985102
inplace : bool, default False

pandas/core/window/rolling.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -680,7 +680,8 @@ def __init__(
680680
# GH 32262: It's convention to keep the grouping column in
681681
# groupby.<agg_func>, but unexpected to users in
682682
# groupby.rolling.<agg_func>
683-
obj = obj.drop(columns=self._grouper.names, errors="ignore")
683+
if obj.ndim == 2:
684+
obj = obj.drop(columns=self._grouper.names, errors="ignore")
684685
# GH 15354
685686
if kwargs.get("step") is not None:
686687
raise NotImplementedError("step not implemented for groupby")
@@ -715,7 +716,7 @@ def _apply(
715716
if key not in self.obj.index.names or key is None
716717
]
717718

718-
if len(drop_columns) != len(groupby_keys):
719+
if len(drop_columns) != len(groupby_keys) and result.ndim == 2:
719720
# Our result will have still kept the column in the result
720721
result = result.drop(columns=drop_columns, errors="ignore")
721722

@@ -758,8 +759,9 @@ def _apply_pairwise(
758759
"""
759760
Apply the given pairwise function given 2 pandas objects (DataFrame/Series)
760761
"""
761-
# Manually drop the grouping column first
762-
target = target.drop(columns=self._grouper.names, errors="ignore")
762+
if target.ndim == 2:
763+
# Manually drop the grouping column first
764+
target = target.drop(columns=self._grouper.names, errors="ignore")
763765
result = super()._apply_pairwise(target, other, pairwise, func, numeric_only)
764766
# 1) Determine the levels + codes of the groupby levels
765767
if other is not None and not all(

pandas/tests/series/methods/test_drop.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
import pytest
22

3+
from pandas.errors import Pandas4Warning
4+
35
from pandas import (
46
Index,
57
Series,
@@ -97,3 +99,12 @@ def test_drop_index_ea_dtype(any_numeric_ea_dtype):
9799
result = df.drop(idx)
98100
expected = Series(100, index=Index([1], dtype=any_numeric_ea_dtype))
99101
tm.assert_series_equal(result, expected)
102+
103+
104+
def test_drop_series_columns_deprecated():
105+
# GH#39509
106+
ser = Series({"a": 1, "b": 3})
107+
108+
msg = "the 'columns' keyword is deprecated and will be removed"
109+
with tm.assert_produces_warning(Pandas4Warning, match=msg):
110+
ser.drop(columns=["a"])

0 commit comments

Comments
 (0)