Skip to content

Commit 2e4bd7b

Browse files
topper-123No-Stream
authored andcommitted
DOC: Add examples for MultiIndex.get_locs + cleanups (pandas-dev#17675)
1 parent 0ec5f44 commit 2e4bd7b

File tree

2 files changed

+47
-20
lines changed

2 files changed

+47
-20
lines changed

pandas/core/categorical.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -229,7 +229,7 @@ class Categorical(PandasObject):
229229
230230
See also
231231
--------
232-
pandas.api.types.CategoricalDtype
232+
pandas.api.types.CategoricalDtype : Type for categorical data
233233
CategoricalIndex : An Index with an underlying ``Categorical``
234234
"""
235235

pandas/core/indexes/multi.py

+46-19
Original file line numberDiff line numberDiff line change
@@ -72,8 +72,8 @@ class MultiIndex(Index):
7272
Examples
7373
---------
7474
A new ``MultiIndex`` is typically constructed using one of the helper
75-
methods :meth:`MultiIndex.from_arrays``, :meth:`MultiIndex.from_product``
76-
and :meth:`MultiIndex.from_tuples``. For example (using ``.from_arrays``):
75+
methods :meth:`MultiIndex.from_arrays`, :meth:`MultiIndex.from_product`
76+
and :meth:`MultiIndex.from_tuples`. For example (using ``.from_arrays``):
7777
7878
>>> arrays = [[1, 1, 2, 2], ['red', 'blue', 'red', 'blue']]
7979
>>> pd.MultiIndex.from_arrays(arrays, names=('number', 'color'))
@@ -1982,33 +1982,41 @@ def _partial_tup_index(self, tup, side='left'):
19821982

19831983
def get_loc(self, key, method=None):
19841984
"""
1985-
Get integer location, slice or boolean mask for requested label or
1986-
tuple. If the key is past the lexsort depth, the return may be a
1987-
boolean mask array, otherwise it is always a slice or int.
1985+
Get location for a label or a tuple of labels as an integer, slice or
1986+
boolean mask.
19881987
19891988
Parameters
19901989
----------
1991-
key : label or tuple
1990+
key : label or tuple of labels (one for each level)
19921991
method : None
19931992
19941993
Returns
19951994
-------
19961995
loc : int, slice object or boolean mask
1996+
If the key is past the lexsort depth, the return may be a
1997+
boolean mask array, otherwise it is always a slice or int.
19971998
19981999
Examples
19992000
---------
20002001
>>> mi = pd.MultiIndex.from_arrays([list('abb'), list('def')])
2002+
20012003
>>> mi.get_loc('b')
20022004
slice(1, 3, None)
2005+
20032006
>>> mi.get_loc(('b', 'e'))
20042007
1
20052008
2009+
Notes
2010+
------
2011+
The key cannot be a slice, list of same-level labels, a boolean mask,
2012+
or a sequence of such. If you want to use those, use
2013+
:meth:`MultiIndex.get_locs` instead.
2014+
20062015
See also
20072016
--------
20082017
Index.get_loc : get_loc method for (single-level) index.
2009-
get_locs : Given a tuple of slices/lists/labels/boolean indexer to a
2010-
level-wise spec, produce an indexer to extract those
2011-
locations.
2018+
MultiIndex.get_locs : Get location for a label/slice/list/mask or a
2019+
sequence of such.
20122020
"""
20132021
if method is not None:
20142022
raise NotImplementedError('only the default get_loc method is '
@@ -2117,8 +2125,9 @@ def get_loc_level(self, key, level=0, drop_level=True):
21172125
21182126
See Also
21192127
---------
2120-
MultiIndex.get_loc : Get integer location, slice or boolean mask for
2121-
requested label or tuple.
2128+
MultiIndex.get_loc : Get location for a label or a tuple of labels.
2129+
MultiIndex.get_locs : Get location for a label/slice/list/mask or a
2130+
sequence of such
21222131
"""
21232132

21242133
def maybe_droplevels(indexer, levels, drop_level):
@@ -2328,23 +2337,41 @@ def convert_indexer(start, stop, step, indexer=indexer, labels=labels):
23282337
j = labels.searchsorted(loc, side='right')
23292338
return slice(i, j)
23302339

2331-
def get_locs(self, tup):
2340+
def get_locs(self, seq):
23322341
"""
2333-
Given a tuple of slices/lists/labels/boolean indexer to a level-wise
2334-
spec produce an indexer to extract those locations
2342+
Get location for a given label/slice/list/mask or a sequence of such as
2343+
an array of integers.
23352344
23362345
Parameters
23372346
----------
2338-
key : tuple of (slices/list/labels)
2347+
seq : label/slice/list/mask or a sequence of such
2348+
You should use one of the above for each level.
2349+
If a level should not be used, set it to ``slice(None)``.
23392350
23402351
Returns
23412352
-------
2342-
locs : integer list of locations or boolean indexer suitable
2343-
for passing to iloc
2353+
locs : array of integers suitable for passing to iloc
2354+
2355+
Examples
2356+
---------
2357+
>>> mi = pd.MultiIndex.from_arrays([list('abb'), list('def')])
2358+
2359+
>>> mi.get_locs('b')
2360+
array([1, 2], dtype=int64)
2361+
2362+
>>> mi.get_locs([slice(None), ['e', 'f']])
2363+
array([1, 2], dtype=int64)
2364+
2365+
>>> mi.get_locs([[True, False, True], slice('e', 'f')])
2366+
array([2], dtype=int64)
2367+
2368+
See also
2369+
--------
2370+
MultiIndex.get_loc : Get location for a label or a tuple of labels.
23442371
"""
23452372

23462373
# must be lexsorted to at least as many levels
2347-
true_slices = [i for (i, s) in enumerate(is_true_slices(tup)) if s]
2374+
true_slices = [i for (i, s) in enumerate(is_true_slices(seq)) if s]
23482375
if true_slices and true_slices[-1] >= self.lexsort_depth:
23492376
raise UnsortedIndexError('MultiIndex slicing requires the index '
23502377
'to be lexsorted: slicing on levels {0}, '
@@ -2377,7 +2404,7 @@ def _update_indexer(idxr, indexer=indexer):
23772404
return indexer
23782405
return indexer & idxr
23792406

2380-
for i, k in enumerate(tup):
2407+
for i, k in enumerate(seq):
23812408

23822409
if is_bool_indexer(k):
23832410
# a boolean indexer, must be the same length!

0 commit comments

Comments
 (0)