pandas.Index.slice_indexer#
- Index.slice_indexer(start=None, end=None, step=None)[source]#
Compute the slice indexer for input labels and step.
Index needs to be ordered and unique.
- Parameters:
- startlabel, default None
If None, defaults to the beginning.
- endlabel, default None
If None, defaults to the end.
- stepint, default None
If None, defaults to 1.
- Returns:
- slice
A slice object.
- Raises:
- KeyErrorIf key does not exist, or key is not unique and index is
not ordered.
See also
Index.slice_locs
Computes slice locations for input labels.
Index.get_slice_bound
Retrieves slice bound that corresponds to given label.
Notes
This function assumes that the data is sorted, so use at your own peril.
Examples
This is a method on all index types. For example you can do:
>>> idx = pd.Index(list("abcd")) >>> idx.slice_indexer(start="b", end="c") slice(1, 3, None)
>>> idx = pd.MultiIndex.from_arrays([list("abcd"), list("efgh")]) >>> idx.slice_indexer(start="b", end=("c", "g")) slice(1, 3, None)