Skip to content

Commit 7b174e5

Browse files
TanyaaCJainPingviinituutti
authored andcommitted
DOC: Improve the docstring of pd.Index.contains and closes PR pandas-dev#20211 (pandas-dev#23100)
1 parent 37a07ce commit 7b174e5

File tree

5 files changed

+34
-21
lines changed

5 files changed

+34
-21
lines changed

pandas/core/indexes/base.py

+30-17
Original file line numberDiff line numberDiff line change
@@ -3812,38 +3812,51 @@ def _is_memory_usage_qualified(self):
38123812
def is_type_compatible(self, kind):
38133813
return kind == self.inferred_type
38143814

3815-
_index_shared_docs['__contains__'] = """
3816-
Return a boolean if this key is IN the index.
3815+
_index_shared_docs['contains'] = """
3816+
Return a boolean indicating whether the provided key is in the index.
38173817
38183818
Parameters
38193819
----------
3820-
key : object
3820+
key : label
3821+
The key to check if it is present in the index.
38213822
38223823
Returns
38233824
-------
3824-
boolean
3825+
bool
3826+
Whether the key search is in the index.
3827+
3828+
See Also
3829+
--------
3830+
Index.isin : Returns an ndarray of boolean dtype indicating whether the
3831+
list-like key is in the index.
3832+
3833+
Examples
3834+
--------
3835+
>>> idx = pd.Index([1, 2, 3, 4])
3836+
>>> idx
3837+
Int64Index([1, 2, 3, 4], dtype='int64')
3838+
3839+
>>> idx.contains(2)
3840+
True
3841+
>>> idx.contains(6)
3842+
False
3843+
3844+
This is equivalent to:
3845+
3846+
>>> 2 in idx
3847+
True
3848+
>>> 6 in idx
3849+
False
38253850
"""
38263851

3827-
@Appender(_index_shared_docs['__contains__'] % _index_doc_kwargs)
3852+
@Appender(_index_shared_docs['contains'] % _index_doc_kwargs)
38283853
def __contains__(self, key):
38293854
hash(key)
38303855
try:
38313856
return key in self._engine
38323857
except (OverflowError, TypeError, ValueError):
38333858
return False
38343859

3835-
_index_shared_docs['contains'] = """
3836-
Return a boolean if this key is IN the index.
3837-
3838-
Parameters
3839-
----------
3840-
key : object
3841-
3842-
Returns
3843-
-------
3844-
boolean
3845-
"""
3846-
38473860
@Appender(_index_shared_docs['contains'] % _index_doc_kwargs)
38483861
def contains(self, key):
38493862
hash(key)

pandas/core/indexes/category.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -359,7 +359,7 @@ def ordered(self):
359359
def _reverse_indexer(self):
360360
return self._data._reverse_indexer()
361361

362-
@Appender(_index_shared_docs['__contains__'] % _index_doc_kwargs)
362+
@Appender(_index_shared_docs['contains'] % _index_doc_kwargs)
363363
def __contains__(self, key):
364364
# if key is a NaN, check if any NaN is in self.
365365
if isna(key):

pandas/core/indexes/datetimelike.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,7 @@ def _box_values_as_index(self):
150150
from pandas.core.index import Index
151151
return Index(self._box_values(self.asi8), name=self.name, dtype=object)
152152

153-
@Appender(_index_shared_docs['__contains__'] % _index_doc_kwargs)
153+
@Appender(_index_shared_docs['contains'] % _index_doc_kwargs)
154154
def __contains__(self, key):
155155
try:
156156
res = self.get_loc(key)

pandas/core/indexes/multi.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -755,7 +755,7 @@ def _shallow_copy_with_infer(self, values, **kwargs):
755755
**kwargs)
756756
return self._shallow_copy(values, **kwargs)
757757

758-
@Appender(_index_shared_docs['__contains__'] % _index_doc_kwargs)
758+
@Appender(_index_shared_docs['contains'] % _index_doc_kwargs)
759759
def __contains__(self, key):
760760
hash(key)
761761
try:

pandas/core/indexes/period.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -400,7 +400,7 @@ def _mpl_repr(self):
400400
def _engine(self):
401401
return self._engine_type(lambda: self, len(self))
402402

403-
@Appender(_index_shared_docs['__contains__'])
403+
@Appender(_index_shared_docs['contains'])
404404
def __contains__(self, key):
405405
if isinstance(key, Period):
406406
if key.freq != self.freq:

0 commit comments

Comments
 (0)