Skip to content

DOC: Improve the docstring of pd.Index.contains and closes PR #20211 #23100

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 8 commits into from
Dec 7, 2018
26 changes: 23 additions & 3 deletions pandas/core/indexes/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -2005,15 +2005,35 @@ def __contains__(self, key):
return False

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

Choose a reason for hiding this comment

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

Minor thing, and I know this is the original description and not yours, but this sounds like it a boolean is returned only if the key is in the index. Not that a boolean is always returned, on whether the key is in the index. Do you mind rephrasing it to be more correct please?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Sure, I'll rephrase it in the next commit as a boolean indicator. Apparently, there are few more docstrings with similar phrasing like Series.is_unique, Series.is_monotonic, Series.is_monotonic_increasing, Series.is_monotonic_decreasing. These too can be corrected in future if seems appropriate.


Parameters
----------
key : object
key : label
The key can be of the same type as the label of :class: `Index`,
Copy link
Member

Choose a reason for hiding this comment

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

I think the space after :class: is not needed. Did you check if this is rendered correctly in the html version? You should be able to generate this page with ./doc/make.py html --single=pandas.Index.contains

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thanks, it renders correctly after removing the space after :class:

hence immutable-like and 1-dimensional if it is a tuple.

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

See Also
--------
Index.isin : Returns ndarray of boolean dtype if list-like key is in
index.

Examples
--------
>>> idx = pd.Index([1, 2, (3, 4), 5])
Copy link
Contributor

Choose a reason for hiding this comment

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

I would prefer an example with a simple values like pd.Index(list('abcd')). This is a rather esoteric case.

>>> idx
Index([1, 2, (3, 4), 5], dtype='object')
>>> idx.contains((3,4))
True
Copy link
Member

Choose a reason for hiding this comment

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

Small thing, but do you mind moving this example after the other two? I think it helps if we show the simple/easy cases first, and the more complex later.

Also, you there is a missing space after the comma.

When you're done with the changes, it may be helpful to run ./scripts/validate_docstrings.py pandas.Index.contains to make sure there are not errors (I don't think there are).

And also flake8 --doctests pandas/core/indexes/base.py. Note that this will report all doctests problems in all the docstrings of the file. Just look that there is no problem in the lines of this docstring 2008-2036.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I have made the said changes.

Validation tests passed with a warning of absence of an extended description which has been suggested to avoid in the review of the original PR. Tested via command ./scripts/validate_docstrings.py pandas.Index.contains

No doctests problem were reported for lines 2008-2036 by testing via command flake8 --doctests pandas/core/indexes/base.py

>>> idx.contains(1)
True
>>> idx.contains(6)
False
"""

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