Skip to content

Commit add74a7

Browse files
MarcoGorelliJulianWgs
authored andcommitted
Deprecate non-keyword arguments in drop (pandas-dev#41486)
1 parent ffdfb13 commit add74a7

File tree

7 files changed

+36
-6
lines changed

7 files changed

+36
-6
lines changed

doc/source/whatsnew/v1.3.0.rst

+1
Original file line numberDiff line numberDiff line change
@@ -693,6 +693,7 @@ Deprecations
693693
- Deprecated passing arguments as positional in :meth:`DataFrame.reset_index` (other than ``"level"``) and :meth:`Series.reset_index` (:issue:`41485`)
694694
- 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`)
695695
- Deprecated passing arguments as positional in :meth:`DataFrame.where` and :meth:`Series.where` (other than ``"cond"`` and ``"other"``) (:issue:`41485`)
696+
- Deprecated passing arguments as positional in :meth:`DataFrame.drop` (other than ``"labels"``) and :meth:`Series.drop` (:issue:`41485`)
696697
-
697698

698699
.. _whatsnew_130.deprecations.nuisance_columns:

pandas/core/frame.py

+1
Original file line numberDiff line numberDiff line change
@@ -4766,6 +4766,7 @@ def reindex(self, *args, **kwargs) -> DataFrame:
47664766
kwargs.pop("labels", None)
47674767
return super().reindex(**kwargs)
47684768

4769+
@deprecate_nonkeyword_arguments(version=None, allowed_args=["self", "labels"])
47694770
def drop(
47704771
self,
47714772
labels=None,

pandas/core/series.py

+1
Original file line numberDiff line numberDiff line change
@@ -4522,6 +4522,7 @@ def set_axis(self, labels, axis: Axis = 0, inplace: bool = False):
45224522
def reindex(self, index=None, **kwargs):
45234523
return super().reindex(index=index, **kwargs)
45244524

4525+
@deprecate_nonkeyword_arguments(version=None, allowed_args=["self", "labels"])
45254526
def drop(
45264527
self,
45274528
labels=None,

pandas/io/stata.py

+3-1
Original file line numberDiff line numberDiff line change
@@ -1761,7 +1761,9 @@ def _do_convert_missing(self, data: DataFrame, convert_missing: bool) -> DataFra
17611761
if replacements:
17621762
columns = data.columns
17631763
replacement_df = DataFrame(replacements)
1764-
replaced = concat([data.drop(replacement_df.columns, 1), replacement_df], 1)
1764+
replaced = concat(
1765+
[data.drop(replacement_df.columns, axis=1), replacement_df], 1
1766+
)
17651767
data = replaced[columns]
17661768
return data
17671769

pandas/tests/frame/methods/test_drop.py

+16-4
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ def test_drop_names(self):
8989
with pytest.raises(KeyError, match=msg):
9090
df.drop(["g"])
9191
with pytest.raises(KeyError, match=msg):
92-
df.drop(["g"], 1)
92+
df.drop(["g"], axis=1)
9393

9494
# errors = 'ignore'
9595
dropped = df.drop(["g"], errors="ignore")
@@ -123,11 +123,11 @@ def test_drop(self):
123123
with pytest.raises(KeyError, match=r"\[5\] not found in axis"):
124124
simple.drop(5)
125125
with pytest.raises(KeyError, match=r"\['C'\] not found in axis"):
126-
simple.drop("C", 1)
126+
simple.drop("C", axis=1)
127127
with pytest.raises(KeyError, match=r"\[5\] not found in axis"):
128128
simple.drop([1, 5])
129129
with pytest.raises(KeyError, match=r"\['C'\] not found in axis"):
130-
simple.drop(["A", "C"], 1)
130+
simple.drop(["A", "C"], axis=1)
131131

132132
# errors = 'ignore'
133133
tm.assert_frame_equal(simple.drop(5, errors="ignore"), simple)
@@ -201,7 +201,7 @@ def test_drop_api_equivalence(self):
201201
res2 = df.drop(index="a")
202202
tm.assert_frame_equal(res1, res2)
203203

204-
res1 = df.drop("d", 1)
204+
res1 = df.drop("d", axis=1)
205205
res2 = df.drop(columns="d")
206206
tm.assert_frame_equal(res1, res2)
207207

@@ -482,6 +482,18 @@ def test_drop_with_duplicate_columns2(self):
482482
result = df2.drop("C", axis=1)
483483
tm.assert_frame_equal(result, expected)
484484

485+
def test_drop_pos_args_deprecation(self):
486+
# https://github.com/pandas-dev/pandas/issues/41485
487+
df = DataFrame({"a": [1, 2, 3]})
488+
msg = (
489+
r"In a future version of pandas all arguments of DataFrame\.drop "
490+
r"except for the argument 'labels' will be keyword-only"
491+
)
492+
with tm.assert_produces_warning(FutureWarning, match=msg):
493+
result = df.drop("a", 1)
494+
expected = DataFrame(index=[0, 1, 2])
495+
tm.assert_frame_equal(result, expected)
496+
485497
def test_drop_inplace_no_leftover_column_reference(self):
486498
# GH 13934
487499
df = DataFrame({"a": [1, 2, 3]})

pandas/tests/groupby/transform/test_transform.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -901,7 +901,7 @@ def test_pad_stable_sorting(fill_method):
901901
y = y[::-1]
902902

903903
df = DataFrame({"x": x, "y": y})
904-
expected = df.drop("x", 1)
904+
expected = df.drop("x", axis=1)
905905

906906
result = getattr(df.groupby("x"), fill_method)()
907907

pandas/tests/series/methods/test_drop.py

+13
Original file line numberDiff line numberDiff line change
@@ -84,3 +84,16 @@ def test_drop_non_empty_list(data, index, drop_labels):
8484
ser = Series(data=data, index=index, dtype=dtype)
8585
with pytest.raises(KeyError, match="not found in axis"):
8686
ser.drop(drop_labels)
87+
88+
89+
def test_drop_pos_args_deprecation():
90+
# https://github.com/pandas-dev/pandas/issues/41485
91+
ser = Series([1, 2, 3])
92+
msg = (
93+
r"In a future version of pandas all arguments of Series\.drop "
94+
r"except for the argument 'labels' will be keyword-only"
95+
)
96+
with tm.assert_produces_warning(FutureWarning, match=msg):
97+
result = ser.drop(1, 0)
98+
expected = Series([1, 3], index=[0, 2])
99+
tm.assert_series_equal(result, expected)

0 commit comments

Comments
 (0)