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 @@ -682,6 +682,7 @@ Deprecations
- Deprecated passing arguments as positional in :meth:`DataFrame.clip` and :meth:`Series.clip` (other than ``"upper"`` and ``"lower"``) (:issue:`41485`)
- Deprecated special treatment of lists with first element a Categorical in the :class:`DataFrame` constructor; pass as ``pd.DataFrame({col: categorical, ...})`` instead (:issue:`38845`)
- Deprecated passing arguments as positional (except for ``"method"``) in :meth:`DataFrame.interpolate` and :meth:`Series.interpolate` (:issue:`41485`)
- Deprecated passing arguments as positional in :meth:`DataFrame.drop` (other than ``"labels"``) and :meth:`Series.drop` (:issue:`41485`)
- Deprecated passing arguments as positional in :meth:`DataFrame.sort_values` (other than ``"by"``) and :meth:`Series.sort_values` (:issue:`41485`)
- Deprecated passing arguments as positional in :meth:`DataFrame.dropna` and :meth:`Series.dropna` (:issue:`41485`)
- Deprecated passing arguments as positional in :meth:`DataFrame.set_index` (other than ``"keys"``) (:issue:`41485`)
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)