Skip to content

DOC: add docstring for Index.get_duplicates #20223

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

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 53 additions & 0 deletions pandas/core/indexes/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -1710,6 +1710,59 @@ def _invalid_indexer(self, form, key):
kind=type(key)))

def get_duplicates(self):
"""
Extract duplicated index elements.

Returns a sorted list of index elements which appear more than once in
the index.

Returns
-------
array-like
List of duplicated indexes.

See Also
--------
Index.duplicated : Return boolean array denoting duplicates.
Index.drop_duplicates : Return Index with duplicates removed.

Examples
--------

Works on different Index of types.

>>> pd.Index([1, 2, 2, 3, 3, 3, 4]).get_duplicates()
[2, 3]
>>> pd.Index([1., 2., 2., 3., 3., 3., 4.]).get_duplicates()
[2.0, 3.0]
>>> pd.Index(['a', 'b', 'b', 'c', 'c', 'c', 'd']).get_duplicates()
['b', 'c']
>>> dates = pd.to_datetime(['2018-01-01', '2018-01-02', '2018-01-03',
... '2018-01-03', '2018-01-04', '2018-01-04'],
... format='%Y-%m-%d')
>>> pd.Index(dates).get_duplicates()
DatetimeIndex(['2018-01-03', '2018-01-04'],
dtype='datetime64[ns]', freq=None)

Sorts duplicated elements even when indexes are unordered.

>>> pd.Index([1, 2, 3, 2, 3, 4, 3]).get_duplicates()
[2, 3]

Return empty array-like structure when all elements are unique.

>>> pd.Index([1, 2, 3, 4]).get_duplicates()
[]
>>> dates = pd.to_datetime(['2018-01-01', '2018-01-02', '2018-01-03'],
... format='%Y-%m-%d')
>>> pd.Index(dates).get_duplicates()
DatetimeIndex([], dtype='datetime64[ns]', freq=None)

Notes
-----
In case of datetime-like indexes, the function is overridden where the
result is converted to DatetimeIndex.
Copy link
Member

Choose a reason for hiding this comment

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

This note is not really clear to me. What do you want to explain?
You already show above that the return value in case of DatetimeIndex is different.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Basically, I wanted to explain why get_duplicates return a DatetimeIndex instead of a list when dates are used.

When dates are passed to the Index class, the DatetimeIndexOpsMixin.get_duplicates function is used. This function calls the Index.get_duplicates and converts the results to DatetimeIndex.

I did not want to get into such details, since I could not mention private classes in the docstring.

Should I get into even less detail? I was wondering whether the following note is enough:
"In case of datetime-like indexes, DatetimeIndex is returned instead of a list."

Copy link
Member

Choose a reason for hiding this comment

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

OK, I understand. What is important to the user is what is returned, and you already show in the examples that for DatetimeIndex a DatetimeIndex and not a list is returned.
The fact that this internally is implemented by overwriting that method in DatetimeIndex is not really important for users, so I would then just remove this comment.

Feel free to open a PR for it!

"""
from collections import defaultdict
counter = defaultdict(lambda: 0)
for k in self.values:
Expand Down