Skip to content

Commit 4a43815

Browse files
noemieltetoTomAugspurger
authored andcommitted
DOC: update the Index.isin docstring (#20249)
1 parent aaa4d90 commit 4a43815

File tree

2 files changed

+201615
-6
lines changed

2 files changed

+201615
-6
lines changed

pandas/core/indexes/base.py

+60-6
Original file line numberDiff line numberDiff line change
@@ -3396,8 +3396,11 @@ def map(self, mapper, na_action=None):
33963396

33973397
def isin(self, values, level=None):
33983398
"""
3399+
Return a boolean array where the index values are in `values`.
3400+
33993401
Compute boolean array of whether each index value is found in the
3400-
passed set of values.
3402+
passed set of values. The length of the returned boolean array matches
3403+
the length of the index.
34013404
34023405
Parameters
34033406
----------
@@ -3406,23 +3409,74 @@ def isin(self, values, level=None):
34063409
34073410
.. versionadded:: 0.18.1
34083411
3409-
Support for values as a set
3412+
Support for values as a set.
34103413
34113414
level : str or int, optional
34123415
Name or position of the index level to use (if the index is a
3413-
MultiIndex).
3416+
`MultiIndex`).
3417+
3418+
Returns
3419+
-------
3420+
is_contained : ndarray
3421+
NumPy array of boolean values.
3422+
3423+
See also
3424+
--------
3425+
Series.isin : Same for Series.
3426+
DataFrame.isin : Same method for DataFrames.
34143427
34153428
Notes
34163429
-----
3430+
In the case of `MultiIndex` you must either specify `values` as a
3431+
list-like object containing tuples that are the same length as the
3432+
number of levels, or specify `level`. Otherwise it will raise a
3433+
``ValueError``.
3434+
34173435
If `level` is specified:
34183436
34193437
- if it is the name of one *and only one* index level, use that level;
34203438
- otherwise it should be a number indicating level position.
34213439
3422-
Returns
3423-
-------
3424-
is_contained : ndarray (boolean dtype)
3440+
Examples
3441+
--------
3442+
>>> idx = pd.Index([1,2,3])
3443+
>>> idx
3444+
Int64Index([1, 2, 3], dtype='int64')
3445+
3446+
Check whether each index value in a list of values.
3447+
>>> idx.isin([1, 4])
3448+
array([ True, False, False])
3449+
3450+
>>> midx = pd.MultiIndex.from_arrays([[1,2,3],
3451+
... ['red', 'blue', 'green']],
3452+
... names=('number', 'color'))
3453+
>>> midx
3454+
MultiIndex(levels=[[1, 2, 3], ['blue', 'green', 'red']],
3455+
labels=[[0, 1, 2], [2, 0, 1]],
3456+
names=['number', 'color'])
3457+
3458+
Check whether the strings in the 'color' level of the MultiIndex
3459+
are in a list of colors.
3460+
3461+
>>> midx.isin(['red', 'orange', 'yellow'], level='color')
3462+
array([ True, False, False])
3463+
3464+
To check across the levels of a MultiIndex, pass a list of tuples:
3465+
3466+
>>> midx.isin([(1, 'red'), (3, 'red')])
3467+
array([ True, False, False])
3468+
3469+
For a DatetimeIndex, string values in `values` are converted to
3470+
Timestamps.
3471+
3472+
>>> dates = ['2000-03-11', '2000-03-12', '2000-03-13']
3473+
>>> dti = pd.to_datetime(dates)
3474+
>>> dti
3475+
DatetimeIndex(['2000-03-11', '2000-03-12', '2000-03-13'],
3476+
dtype='datetime64[ns]', freq=None)
34253477
3478+
>>> dti.isin(['2000-03-11'])
3479+
array([ True, False, False])
34263480
"""
34273481
if level is not None:
34283482
self._validate_index_level(level)

0 commit comments

Comments
 (0)