Skip to content

Commit 064268f

Browse files
committed
ENH: Deprecate non-keyword arguments for drop_duplicates.
1 parent 56a8abe commit 064268f

File tree

4 files changed

+37
-3
lines changed

4 files changed

+37
-3
lines changed

doc/source/whatsnew/v1.3.0.rst

+1-3
Original file line numberDiff line numberDiff line change
@@ -676,11 +676,9 @@ Deprecations
676676
- Deprecated using ``usecols`` with out of bounds indices for ``read_csv`` with ``engine="c"`` (:issue:`25623`)
677677
- Deprecated special treatment of lists with first element a Categorical in the :class:`DataFrame` constructor; pass as ``pd.DataFrame({col: categorical, ...})`` instead (:issue:`38845`)
678678
- Deprecated passing arguments as positional (except for ``"method"``) in :meth:`DataFrame.interpolate` and :meth:`Series.interpolate` (:issue:`41485`)
679-
<<<<<<< HEAD
680679
- Deprecated passing arguments (apart from ``value``) as positional in :meth:`DataFrame.fillna` and :meth:`Series.fillna` (:issue:`41485`)
681680
- 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`)
682-
- Deprecated passing arguments as positional in :meth:`DataFrame.drop_duplicates` (except for ``subset``) and :meth:`Series.drop_duplicates` (:issue:`41485`)
683-
681+
- Deprecated passing arguments as positional in :meth:`DataFrame.drop_duplicates` (except for ``subset``), :meth:`Series.drop_duplicates` and :meth:`Index.drop_duplicates` (:issue:`41485`)
684682

685683
.. ---------------------------------------------------------------------------
686684

pandas/core/indexes/base.py

+2
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@
5454
from pandas.util._decorators import (
5555
Appender,
5656
cache_readonly,
57+
deprecate_nonkeyword_arguments,
5758
doc,
5859
)
5960

@@ -2633,6 +2634,7 @@ def unique(self: _IndexT, level: Hashable | None = None) -> _IndexT:
26332634
result = super().unique()
26342635
return self._shallow_copy(result)
26352636

2637+
@deprecate_nonkeyword_arguments(version=None, allowed_args=["self"])
26362638
@final
26372639
def drop_duplicates(self: _IndexT, keep: str_t | bool = "first") -> _IndexT:
26382640
"""

pandas/tests/indexes/multi/test_duplicates.py

+17
Original file line numberDiff line numberDiff line change
@@ -306,3 +306,20 @@ def test_duplicated_drop_duplicates():
306306
assert duplicated.dtype == bool
307307
expected = MultiIndex.from_arrays(([2, 3, 2, 3], [1, 1, 2, 2]))
308308
tm.assert_index_equal(idx.drop_duplicates(keep=False), expected)
309+
310+
311+
def test_multi_drop_duplicates_pos_args_deprecation():
312+
idx = MultiIndex.from_arrays([[1, 2, 3, 1], [1, 2, 3, 1]])
313+
314+
msg = (
315+
"In a future version of pandas all arguments of "
316+
"Index.drop_duplicates will be keyword-only"
317+
)
318+
319+
with tm.assert_produces_warning(FutureWarning, match=msg):
320+
idx.drop_duplicates("last")
321+
result = idx.drop_duplicates("last")
322+
323+
expected = MultiIndex.from_arrays([[2, 3, 1], [2, 3, 1]])
324+
325+
tm.assert_index_equal(expected, result)

pandas/tests/indexes/test_base.py

+17
Original file line numberDiff line numberDiff line change
@@ -1738,3 +1738,20 @@ def test_construct_from_memoryview(klass, extra_kwargs):
17381738
result = klass(memoryview(np.arange(2000, 2005)), **extra_kwargs)
17391739
expected = klass(range(2000, 2005), **extra_kwargs)
17401740
tm.assert_index_equal(result, expected)
1741+
1742+
1743+
def test_drop_duplicates_pos_args_deprecation():
1744+
idx = Index([1, 2, 3, 1])
1745+
1746+
msg = (
1747+
"In a future version of pandas all arguments of "
1748+
"Index.drop_duplicates will be keyword-only"
1749+
)
1750+
1751+
with tm.assert_produces_warning(FutureWarning, match=msg):
1752+
idx.drop_duplicates("last")
1753+
result = idx.drop_duplicates("last")
1754+
1755+
expected = Index([2, 3, 1])
1756+
1757+
tm.assert_index_equal(expected, result)

0 commit comments

Comments
 (0)