Skip to content

Commit

Permalink
Deprecate non-keyword arguments in drop (#41486)
Browse files Browse the repository at this point in the history
  • Loading branch information
MarcoGorelli authored May 27, 2021
1 parent 4f1b41a commit 22252c9
Show file tree
Hide file tree
Showing 7 changed files with 36 additions and 6 deletions.
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 @@ -693,6 +693,7 @@ Deprecations
- Deprecated passing arguments as positional in :meth:`DataFrame.reset_index` (other than ``"level"``) and :meth:`Series.reset_index` (:issue:`41485`)
- 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`)
- Deprecated passing arguments as positional in :meth:`DataFrame.where` and :meth:`Series.where` (other than ``"cond"`` and ``"other"``) (:issue:`41485`)
- Deprecated passing arguments as positional in :meth:`DataFrame.drop` (other than ``"labels"``) and :meth:`Series.drop` (:issue:`41485`)
-

.. _whatsnew_130.deprecations.nuisance_columns:
Expand Down
1 change: 1 addition & 0 deletions pandas/core/frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -4766,6 +4766,7 @@ def reindex(self, *args, **kwargs) -> DataFrame:
kwargs.pop("labels", None)
return super().reindex(**kwargs)

@deprecate_nonkeyword_arguments(version=None, allowed_args=["self", "labels"])
def drop(
self,
labels=None,
Expand Down
1 change: 1 addition & 0 deletions pandas/core/series.py
Original file line number Diff line number Diff line change
Expand Up @@ -4522,6 +4522,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=None, 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
20 changes: 16 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 @@ -482,6 +482,18 @@ def test_drop_with_duplicate_columns2(self):
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"In a future version of pandas all arguments of DataFrame\.drop "
r"except for the argument 'labels' will be keyword-only"
)
with tm.assert_produces_warning(FutureWarning, match=msg):
result = df.drop("a", 1)
expected = DataFrame(index=[0, 1, 2])
tm.assert_frame_equal(result, expected)

def test_drop_inplace_no_leftover_column_reference(self):
# GH 13934
df = DataFrame({"a": [1, 2, 3]})
Expand Down
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 @@ -901,7 +901,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
13 changes: 13 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,16 @@ 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"In a future version of pandas all arguments of Series\.drop "
r"except for the argument 'labels' will be keyword-only"
)
with tm.assert_produces_warning(FutureWarning, match=msg):
result = ser.drop(1, 0)
expected = Series([1, 3], index=[0, 2])
tm.assert_series_equal(result, expected)

0 comments on commit 22252c9

Please sign in to comment.