@@ -1924,7 +1924,9 @@ def get_slice_bound(self, label, side, kind):
1924
1924
def slice_locs (self , start = None , end = None , step = None , kind = None ):
1925
1925
"""
1926
1926
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
1928
1930
MultiIndex with 3 levels, you can pass a single value (corresponding to
1929
1931
the first level), or a 1-, 2-, or 3-tuple.
1930
1932
@@ -1944,7 +1946,32 @@ def slice_locs(self, start=None, end=None, step=None, kind=None):
1944
1946
1945
1947
Notes
1946
1948
-----
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.
1948
1975
"""
1949
1976
# This function adds nothing to its parent implementation (the magic
1950
1977
# happens in get_slice_bound method), but it adds meaningful doc.
@@ -2015,6 +2042,8 @@ def get_loc(self, key, method=None):
2015
2042
See also
2016
2043
--------
2017
2044
Index.get_loc : get_loc method for (single-level) index.
2045
+ MultiIndex.slice_locs : Get slice location given startlabel(s) and
2046
+ endlabel(s).
2018
2047
MultiIndex.get_locs : Get location for a label/slice/list/mask or a
2019
2048
sequence of such.
2020
2049
"""
@@ -2368,6 +2397,8 @@ def get_locs(self, seq):
2368
2397
See also
2369
2398
--------
2370
2399
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).
2371
2402
"""
2372
2403
2373
2404
# must be lexsorted to at least as many levels
0 commit comments