Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Deprecate non-keyword arguments in drop #41486

Merged
1 change: 1 addition & 0 deletions doc/source/whatsnew/v1.3.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -647,6 +647,7 @@ Deprecations
- Deprecated setting :attr:`Categorical._codes`, create a new :class:`Categorical` with the desired codes instead (:issue:`40606`)
- Deprecated behavior of :meth:`DatetimeIndex.union` with mixed timezones; in a future version both will be cast to UTC instead of object dtype (:issue:`39328`)
- Deprecated using ``usecols`` with out of bounds indices for ``read_csv`` with ``engine="c"`` (:issue:`25623`)
- Deprecated passing arguments (apart from ``labels``) as positional in :meth:`DataFrame.drop` and :meth:`Series.drop` (:issue:`41485`)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

as per other comments, we could consider deprecating some compatibility parameters on Series at the same time.


.. ---------------------------------------------------------------------------

Expand Down
2 changes: 2 additions & 0 deletions pandas/core/frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@
Appender,
Substitution,
deprecate_kwarg,
deprecate_nonkeyword_arguments,
doc,
rewrite_axis_style_signature,
)
Expand Down Expand Up @@ -4738,6 +4739,7 @@ def reindex(self, *args, **kwargs) -> DataFrame:
kwargs.pop("labels", None)
return super().reindex(**kwargs)

@deprecate_nonkeyword_arguments(version="2.0", allowed_args=["self", "labels"])
def drop(
self,
labels=None,
Expand Down
2 changes: 2 additions & 0 deletions pandas/core/series.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@
from pandas.util._decorators import (
Appender,
Substitution,
deprecate_nonkeyword_arguments,
doc,
)
from pandas.util._validators import (
Expand Down Expand Up @@ -4485,6 +4486,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="2.0", allowed_args=["self", "labels"])
def drop(
self,
labels=None,
Expand Down
4 changes: 3 additions & 1 deletion pandas/io/stata.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
18 changes: 14 additions & 4 deletions pandas/tests/frame/methods/test_drop.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)

Expand Down Expand Up @@ -481,3 +481,13 @@ def test_drop_with_duplicate_columns2(self):
df2 = df.take([2, 0, 1, 2, 1], axis=1)
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"Starting with Pandas version 2\.0 all arguments of drop except for the "
r"arguments 'self' and 'labels' will be keyword-only"
)
with tm.assert_produces_warning(FutureWarning, match=msg):
df.drop("a", 1)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you add an assert that the result is still the same as df.drop("a", axis=1) (outside of the assert_produces_warning context)

2 changes: 1 addition & 1 deletion pandas/tests/groupby/transform/test_transform.py
Original file line number Diff line number Diff line change
Expand Up @@ -883,7 +883,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)()

Expand Down
11 changes: 11 additions & 0 deletions pandas/tests/series/methods/test_drop.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,3 +84,14 @@ 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"Starting with Pandas version 2\.0 all arguments of drop except for the "
r"arguments 'self' and 'labels' will be keyword-only"
)
with tm.assert_produces_warning(FutureWarning, match=msg):
ser.drop(1, 0)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same here