Skip to content

DOC: update the pandas.Index.duplicated and pandas.Series.duplicated docstring #20117

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 17 commits into from
Mar 14, 2018
Merged
Show file tree
Hide file tree
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
18 changes: 0 additions & 18 deletions pandas/core/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -1197,24 +1197,6 @@ def drop_duplicates(self, keep='first', inplace=False):
else:
return result

_shared_docs['duplicated'] = (
"""Return boolean %(duplicated)s denoting duplicate values

Parameters
----------
keep : {'first', 'last', False}, default 'first'
- ``first`` : Mark duplicates as ``True`` except for the first
occurrence.
- ``last`` : Mark duplicates as ``True`` except for the last
occurrence.
- False : Mark all duplicates as ``True``.

Returns
-------
duplicated : %(duplicated)s
""")

@Appender(_shared_docs['duplicated'] % _indexops_doc_kwargs)
def duplicated(self, keep='first'):
from pandas.core.algorithms import duplicated
if isinstance(self, ABCIndexClass):
Expand Down
54 changes: 53 additions & 1 deletion pandas/core/indexes/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -4325,8 +4325,60 @@ def drop_duplicates(self, keep='first'):
"""
return super(Index, self).drop_duplicates(keep=keep)

@Appender(base._shared_docs['duplicated'] % _index_doc_kwargs)
def duplicated(self, keep='first'):
"""
Indicate duplicate index values.

Duplicated values are indicated as ``True`` values in the resulting
Copy link
Contributor

Choose a reason for hiding this comment

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

can you coordinate text with #20114, seems some slight differences

Copy link
Member

Choose a reason for hiding this comment

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

Looks like they are in the zone together already :-)
(they are sitting close to me: we removed the extended summary in the other PR as Tom asked, or are there other differences?)

Copy link
Contributor

Choose a reason for hiding this comment

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

great, seemed likely.

array. Either all duplicates, all except the first, or all except the
last occurrence of duplicates can be indicated.

Parameters
----------
keep : {'first', 'last', False}, default 'first'
The value or values in a set of duplicates to mark as missing.

- 'first' : Mark duplicates as ``True`` except for the first
occurrence.
- 'last' : Mark duplicates as ``True`` except for the last
occurrence.
- ``False`` : Mark all duplicates as ``True``.

Examples
--------
By default, for each set of duplicated values, the first occurrence is
set to False and all others to True:

>>> idx = pd.Index(['lama', 'cow', 'lama', 'beetle', 'lama'])
>>> idx.duplicated()
array([False, False, True, False, True])

which is equivalent to

>>> idx.duplicated(keep='first')
array([False, False, True, False, True])

By using 'last', the last occurrence of each set of duplicated values
is set on False and all others on True:

>>> idx.duplicated(keep='last')
array([ True, False, True, False, False])

By setting keep on ``False``, all duplicates are True:

>>> idx.duplicated(keep=False)
array([ True, False, True, False, True])

Returns
-------
numpy.ndarray

See Also
--------
pandas.Series.duplicated : Equivalent method on pandas.Series
pandas.DataFrame.duplicated : Equivalent method on pandas.DataFrame
pandas.Index.drop_duplicates : Remove duplicate values from Index
"""
return super(Index, self).duplicated(keep=keep)

_index_shared_docs['fillna'] = """
Expand Down
2 changes: 1 addition & 1 deletion pandas/core/indexes/category.py
Original file line number Diff line number Diff line change
Expand Up @@ -399,7 +399,7 @@ def unique(self, level=None):
return self._shallow_copy(result, categories=result.categories,
ordered=result.ordered)

@Appender(base._shared_docs['duplicated'] % _index_doc_kwargs)
@Appender(Index.duplicated.__doc__)
def duplicated(self, keep='first'):
from pandas._libs.hashtable import duplicated_int64
codes = self.codes.astype('i8')
Expand Down
2 changes: 1 addition & 1 deletion pandas/core/indexes/multi.py
Original file line number Diff line number Diff line change
Expand Up @@ -916,7 +916,7 @@ def f(k, stringify):
for k, stringify in zip(key, self._have_mixed_levels)])
return hash_tuple(key)

@Appender(base._shared_docs['duplicated'] % _index_doc_kwargs)
@Appender(Index.duplicated.__doc__)
def duplicated(self, keep='first'):
from pandas.core.sorting import get_group_index
from pandas._libs.hashtable import duplicated_int64
Expand Down
72 changes: 71 additions & 1 deletion pandas/core/series.py
Original file line number Diff line number Diff line change
Expand Up @@ -1458,8 +1458,78 @@ def drop_duplicates(self, keep='first', inplace=False):
"""
return super(Series, self).drop_duplicates(keep=keep, inplace=inplace)

@Appender(base._shared_docs['duplicated'] % _shared_doc_kwargs)
def duplicated(self, keep='first'):
"""
Indicate duplicate Series values.

Duplicated values are indicated as ``True`` values in the resulting
Series. Either all duplicates, all except the first or all except the
last occurrence of duplicates can be indicated.

Parameters
----------
keep : {'first', 'last', False}, default 'first'
- 'first' : Mark duplicates as ``True`` except for the first
occurrence.
- 'last' : Mark duplicates as ``True`` except for the last
occurrence.
- ``False`` : Mark all duplicates as ``True``.

Examples
--------
By default, for each set of duplicated values, the first occurrence is
set on False and all others on True:

>>> animals = pd.Series(['lama', 'cow', 'lama', 'beetle', 'lama'])
>>> animals.duplicated()
0 False
1 False
2 True
3 False
4 True
dtype: bool

which is equivalent to

>>> animals.duplicated(keep='first')
0 False
1 False
2 True
3 False
4 True
dtype: bool

By using 'last', the last occurrence of each set of duplicated values
is set on False and all others on True:

>>> animals.duplicated(keep='last')
0 True
1 False
2 True
3 False
4 False
dtype: bool

By setting keep on ``False``, all duplicates are True:

>>> animals.duplicated(keep=False)
0 True
1 False
2 True
3 False
4 True
dtype: bool

Returns
-------
pandas.core.series.Series

See Also
--------
pandas.Index.duplicated : Equivalent method on pandas.Index
pandas.DataFrame.duplicated : Equivalent method on pandas.DataFrame
pandas.Series.drop_duplicates : Remove duplicate values from Series
"""
return super(Series, self).duplicated(keep=keep)

def idxmin(self, axis=None, skipna=True, *args, **kwargs):
Expand Down