Skip to content

DOC: update the pd.Index.contains docstring #20211

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

Closed
wants to merge 1 commit into from
Closed
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
22 changes: 19 additions & 3 deletions pandas/core/indexes/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -1832,15 +1832,31 @@ def __contains__(self, key):
return False

_index_shared_docs['contains'] = """
return a boolean if this key is IN the index
Return a boolean if key is in the index.

This function returns True if key is in the index and
Copy link
Contributor

Choose a reason for hiding this comment

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

I think this extended summary can be removed.

returns False if key is not in index.

Parameters
----------
key : object
key : immutable object
Copy link
Contributor

Choose a reason for hiding this comment

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

can remove immutable, it could be anything.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

can't be anything:
pd.CategoricalIndex([2000, 2001, 2012]).contains([1,2])
TypeError: unhashable type: 'list'

Key to be searched.

Returns
-------
boolean
bool
Result of the key search.

Notes
-----
CategoricalIndex is a sub-class of Index.
Copy link
Contributor

Choose a reason for hiding this comment

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

Don't think you need this Notes section.

Copy link
Contributor

Choose a reason for hiding this comment

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

Could you add a See Also with Index.isin

Copy link
Contributor Author

Choose a reason for hiding this comment

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

this note is because of shared documentation between Index and CategoricalIndex


Examples
--------
>>> pd.CategoricalIndex([2000, 2001, 2012]).contains(2001)
Copy link
Contributor

Choose a reason for hiding this comment

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

Best to just use a regular Index here, not a CategoricalIndex.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

pd.Index.contains and pd.CategoricalIndex.contains have shared documentation
and a CategoricalIndex example is valid for Index but not the other way around since CategoricalIndex is a sub-class of Index

Copy link
Contributor

Choose a reason for hiding this comment

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

yeah for this particular case I would separate the doc-strings, IOW 1 more Index and a separate one for CategoricalIndex. Can use a shared doc if that works (with templates)

True
>>> pd.CategoricalIndex([2000, 2001, 2012]).contains(2222)
False
"""

@Appender(_index_shared_docs['contains'] % _index_doc_kwargs)
Expand Down