Skip to content

Commit b5abe5d

Browse files
authored
ENH: Add ignore_index to Series.drop_duplicates (#50844)
1 parent c1ce0a7 commit b5abe5d

File tree

3 files changed

+31
-4
lines changed

3 files changed

+31
-4
lines changed

doc/source/whatsnew/v2.0.0.rst

+1
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,7 @@ Other enhancements
161161
- Added :meth:`Index.infer_objects` analogous to :meth:`Series.infer_objects` (:issue:`50034`)
162162
- Added ``copy`` parameter to :meth:`Series.infer_objects` and :meth:`DataFrame.infer_objects`, passing ``False`` will avoid making copies for series or columns that are already non-object or where no better dtype can be inferred (:issue:`50096`)
163163
- :meth:`DataFrame.plot.hist` now recognizes ``xlabel`` and ``ylabel`` arguments (:issue:`49793`)
164+
- :meth:`Series.drop_duplicates` has gained ``ignore_index`` keyword to reset index (:issue:`48304`)
164165
- Improved error message in :func:`to_datetime` for non-ISO8601 formats, informing users about the position of the first error (:issue:`50361`)
165166
- Improved error message when trying to align :class:`DataFrame` objects (for example, in :func:`DataFrame.compare`) to clarify that "identically labelled" refers to both index and columns (:issue:`50083`)
166167
- Added :meth:`DatetimeIndex.as_unit` and :meth:`TimedeltaIndex.as_unit` to convert to different resolutions; supported resolutions are "s", "ms", "us", and "ns" (:issue:`50616`)

pandas/core/series.py

+23-4
Original file line numberDiff line numberDiff line change
@@ -2131,22 +2131,32 @@ def unique(self) -> ArrayLike: # pylint: disable=useless-parent-delegation
21312131

21322132
@overload
21332133
def drop_duplicates(
2134-
self, *, keep: DropKeep = ..., inplace: Literal[False] = ...
2134+
self,
2135+
*,
2136+
keep: DropKeep = ...,
2137+
inplace: Literal[False] = ...,
2138+
ignore_index: bool = ...,
21352139
) -> Series:
21362140
...
21372141

21382142
@overload
2139-
def drop_duplicates(self, *, keep: DropKeep = ..., inplace: Literal[True]) -> None:
2143+
def drop_duplicates(
2144+
self, *, keep: DropKeep = ..., inplace: Literal[True], ignore_index: bool = ...
2145+
) -> None:
21402146
...
21412147

21422148
@overload
21432149
def drop_duplicates(
2144-
self, *, keep: DropKeep = ..., inplace: bool = ...
2150+
self, *, keep: DropKeep = ..., inplace: bool = ..., ignore_index: bool = ...
21452151
) -> Series | None:
21462152
...
21472153

21482154
def drop_duplicates(
2149-
self, *, keep: DropKeep = "first", inplace: bool = False
2155+
self,
2156+
*,
2157+
keep: DropKeep = "first",
2158+
inplace: bool = False,
2159+
ignore_index: bool = False,
21502160
) -> Series | None:
21512161
"""
21522162
Return Series with duplicate values removed.
@@ -2163,6 +2173,11 @@ def drop_duplicates(
21632173
inplace : bool, default ``False``
21642174
If ``True``, performs operation inplace and returns None.
21652175
2176+
ignore_index : bool, default ``False``
2177+
If ``True``, the resulting axis will be labeled 0, 1, …, n - 1.
2178+
2179+
.. versionadded:: 2.0.0
2180+
21662181
Returns
21672182
-------
21682183
Series or None
@@ -2225,6 +2240,10 @@ def drop_duplicates(
22252240
"""
22262241
inplace = validate_bool_kwarg(inplace, "inplace")
22272242
result = super().drop_duplicates(keep=keep)
2243+
2244+
if ignore_index:
2245+
result.index = default_index(len(result))
2246+
22282247
if inplace:
22292248
self._update_inplace(result)
22302249
return None

pandas/tests/series/methods/test_drop_duplicates.py

+7
Original file line numberDiff line numberDiff line change
@@ -242,3 +242,10 @@ def test_drop_duplicates_categorical_bool_na(self, nulls_fixture):
242242
index=[0, 1, 4],
243243
)
244244
tm.assert_series_equal(result, expected)
245+
246+
def test_drop_duplicates_ignore_index(self):
247+
# GH#48304
248+
ser = Series([1, 2, 2, 3])
249+
result = ser.drop_duplicates(ignore_index=True)
250+
expected = Series([1, 2, 3])
251+
tm.assert_series_equal(result, expected)

0 commit comments

Comments
 (0)