Skip to content

Commit 36ac61f

Browse files
topper-123No-Stream
authored andcommitted
DOC: Add examples to MultiIndex.slice_locs + note that index.slice requires (pandas-dev#17799)
1 parent 658ca06 commit 36ac61f

File tree

2 files changed

+46
-2
lines changed

2 files changed

+46
-2
lines changed

pandas/core/indexes/base.py

+13
Original file line numberDiff line numberDiff line change
@@ -3589,6 +3589,19 @@ def slice_locs(self, start=None, end=None, step=None, kind=None):
35893589
-------
35903590
start, end : int
35913591
3592+
Notes
3593+
-----
3594+
This method only works if the index is monotonic or unique.
3595+
3596+
Examples
3597+
---------
3598+
>>> idx = pd.Index(list('abcd'))
3599+
>>> idx.slice_locs(start='b', end='c')
3600+
(1, 3)
3601+
3602+
See Also
3603+
--------
3604+
Index.get_loc : Get location for a single label
35923605
"""
35933606
inc = (step is None or step >= 0)
35943607

pandas/core/indexes/multi.py

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

23742405
# must be lexsorted to at least as many levels

0 commit comments

Comments
 (0)