Skip to content

Commit 2874a9e

Browse files
author
tp
committed
Add examples to MultiIndex.slice_locs + note that index.slice requires
monotonic ordering
1 parent 22515f5 commit 2874a9e

File tree

2 files changed

+47
-3
lines changed

2 files changed

+47
-3
lines changed

pandas/core/indexes/base.py

+14-1
Original file line numberDiff line numberDiff line change
@@ -3556,7 +3556,7 @@ def get_slice_bound(self, label, side, kind):
35563556

35573557
def slice_locs(self, start=None, end=None, step=None, kind=None):
35583558
"""
3559-
Compute slice locations for input labels.
3559+
For an ordered index, compute slice locations for input labels.
35603560
35613561
Parameters
35623562
----------
@@ -3572,6 +3572,19 @@ def slice_locs(self, start=None, end=None, step=None, kind=None):
35723572
-------
35733573
start, end : int
35743574
3575+
Notes
3576+
-----
3577+
This method only works if the index is monotonic.
3578+
3579+
Examples
3580+
---------
3581+
>>> idx = pd.Index(list('abcd'))
3582+
>>> idx.slice_locs(start='b', end='c')
3583+
(1, 3)
3584+
3585+
See Also
3586+
--------
3587+
Index.get_loc : Get location for a single label
35753588
"""
35763589
inc = (step is None or step >= 0)
35773590

pandas/core/indexes/multi.py

+33-2
Original file line numberDiff line numberDiff line change
@@ -1924,7 +1924,9 @@ def get_slice_bound(self, label, side, kind):
19241924
def slice_locs(self, start=None, end=None, step=None, kind=None):
19251925
"""
19261926
For an ordered MultiIndex, compute the slice locations for input
1927-
labels. They can be tuples representing partial levels, e.g. for a
1927+
labels.
1928+
1929+
The input labels can be tuples representing partial levels, e.g. for a
19281930
MultiIndex with 3 levels, you can pass a single value (corresponding to
19291931
the first level), or a 1-, 2-, or 3-tuple.
19301932
@@ -1944,7 +1946,32 @@ def slice_locs(self, start=None, end=None, step=None, kind=None):
19441946
19451947
Notes
19461948
-----
1947-
This function assumes that the data is sorted by the first level
1949+
This method only works if the MultiIndex is properly lex-sorted. So,
1950+
if only the first 2 levels of a 3-level MultiIndex are lexsorted,
1951+
you can only pass two levels to ``.slice_locs``.
1952+
1953+
Examples
1954+
--------
1955+
mi = pd.MultiIndex.from_arrays([list('abbd'), list('deff')],
1956+
names=['A', 'B'])
1957+
1958+
Get the slice locations from the beginning of 'b' in the first level
1959+
until the end of the multiindex:
1960+
1961+
mi.slice_locs(start='b')
1962+
(1, 4)
1963+
1964+
Like above, but stop at the end of 'b' in the first level and 'f' in
1965+
the second level:
1966+
1967+
mi.slice_locs(start='b', end=('b', 'f'))
1968+
(1, 3)
1969+
1970+
See Also
1971+
--------
1972+
MultiIndex.get_loc : Get location for a label or a tuple of labels.
1973+
MultiIndex.get_locs : Get location for a label/slice/list/mask or a
1974+
sequence of such.
19481975
"""
19491976
# This function adds nothing to its parent implementation (the magic
19501977
# happens in get_slice_bound method), but it adds meaningful doc.
@@ -2015,6 +2042,8 @@ def get_loc(self, key, method=None):
20152042
See also
20162043
--------
20172044
Index.get_loc : get_loc method for (single-level) index.
2045+
MultiIndex.slice_locs : Get slice location given startlabel(s) and
2046+
endlabel(s).
20182047
MultiIndex.get_locs : Get location for a label/slice/list/mask or a
20192048
sequence of such.
20202049
"""
@@ -2368,6 +2397,8 @@ def get_locs(self, seq):
23682397
See also
23692398
--------
23702399
MultiIndex.get_loc : Get location for a label or a tuple of labels.
2400+
MultiIndex.slice_locs : Get slice location given startlabel(s) and
2401+
endlabel(s).
23712402
"""
23722403

23732404
# must be lexsorted to at least as many levels

0 commit comments

Comments
 (0)