Compute the slice indexer for input labels and step. Index needs to be ordered and unique. Parameters ---------- start : label, default None If None, defaults to the beginning. end : label, default None If None, defaults to t
(
self,
start: Hashable | None = None,
end: Hashable | None = None,
step: int | None = None,
)
| 6751 | raise NotImplementedError |
| 6752 | |
| 6753 | def slice_indexer( |
| 6754 | self, |
| 6755 | start: Hashable | None = None, |
| 6756 | end: Hashable | None = None, |
| 6757 | step: int | None = None, |
| 6758 | ) -> slice: |
| 6759 | """ |
| 6760 | Compute the slice indexer for input labels and step. |
| 6761 | |
| 6762 | Index needs to be ordered and unique. |
| 6763 | |
| 6764 | Parameters |
| 6765 | ---------- |
| 6766 | start : label, default None |
| 6767 | If None, defaults to the beginning. |
| 6768 | end : label, default None |
| 6769 | If None, defaults to the end. |
| 6770 | step : int, default None |
| 6771 | If None, defaults to 1. |
| 6772 | |
| 6773 | Returns |
| 6774 | ------- |
| 6775 | slice |
| 6776 | A slice object. |
| 6777 | |
| 6778 | Raises |
| 6779 | ------ |
| 6780 | KeyError : If key does not exist, or key is not unique and index is |
| 6781 | not ordered. |
| 6782 | |
| 6783 | See Also |
| 6784 | -------- |
| 6785 | Index.slice_locs : Computes slice locations for input labels. |
| 6786 | Index.get_slice_bound : Retrieves slice bound that corresponds to given label. |
| 6787 | |
| 6788 | Notes |
| 6789 | ----- |
| 6790 | This function assumes that the data is sorted, so use at your own peril. |
| 6791 | |
| 6792 | Examples |
| 6793 | -------- |
| 6794 | This is a method on all index types. For example you can do: |
| 6795 | |
| 6796 | >>> idx = pd.Index(list("abcd")) |
| 6797 | >>> idx.slice_indexer(start="b", end="c") |
| 6798 | slice(1, 3, None) |
| 6799 | |
| 6800 | >>> idx = pd.MultiIndex.from_arrays([list("abcd"), list("efgh")]) |
| 6801 | >>> idx.slice_indexer(start="b", end=("c", "g")) |
| 6802 | slice(1, 3, None) |
| 6803 | """ |
| 6804 | start_slice, end_slice = self.slice_locs(start, end, step=step) |
| 6805 | |
| 6806 | # return a slice |
| 6807 | if not is_scalar(start_slice): |
| 6808 | raise AssertionError("Start slice bound is non-scalar") |
| 6809 | if not is_scalar(end_slice): |
| 6810 | raise AssertionError("End slice bound is non-scalar") |