Compute slice locations for input labels. Parameters ---------- start : label, default None If None, defaults to the beginning. end : label, default None If None, defaults to the end. step : int, defaults None If N
(
self,
start: SliceType = None,
end: SliceType = None,
step: int | None = None,
)
| 6998 | return slc |
| 6999 | |
| 7000 | def slice_locs( |
| 7001 | self, |
| 7002 | start: SliceType = None, |
| 7003 | end: SliceType = None, |
| 7004 | step: int | None = None, |
| 7005 | ) -> tuple[int, int]: |
| 7006 | """ |
| 7007 | Compute slice locations for input labels. |
| 7008 | |
| 7009 | Parameters |
| 7010 | ---------- |
| 7011 | start : label, default None |
| 7012 | If None, defaults to the beginning. |
| 7013 | end : label, default None |
| 7014 | If None, defaults to the end. |
| 7015 | step : int, defaults None |
| 7016 | If None, defaults to 1. |
| 7017 | |
| 7018 | Returns |
| 7019 | ------- |
| 7020 | tuple[int, int] |
| 7021 | Returns a tuple of two integers representing the slice locations for the |
| 7022 | input labels within the index. |
| 7023 | |
| 7024 | See Also |
| 7025 | -------- |
| 7026 | Index.get_loc : Get location for a single label. |
| 7027 | |
| 7028 | Notes |
| 7029 | ----- |
| 7030 | This method only works if the index is monotonic or unique. |
| 7031 | |
| 7032 | Examples |
| 7033 | -------- |
| 7034 | >>> idx = pd.Index(list("abcd")) |
| 7035 | >>> idx.slice_locs(start="b", end="c") |
| 7036 | (1, 3) |
| 7037 | |
| 7038 | >>> idx = pd.Index(list("bcde")) |
| 7039 | >>> idx.slice_locs(start="a", end="c") |
| 7040 | (0, 2) |
| 7041 | """ |
| 7042 | inc = step is None or step >= 0 |
| 7043 | |
| 7044 | if not inc: |
| 7045 | # If it's a reverse slice, temporarily swap bounds. |
| 7046 | start, end = end, start |
| 7047 | |
| 7048 | # GH 16785: If start and end happen to be date strings with UTC offsets |
| 7049 | # attempt to parse and check that the offsets are the same |
| 7050 | if isinstance(start, (str, datetime)) and isinstance(end, (str, datetime)): |
| 7051 | try: |
| 7052 | ts_start = Timestamp(start) |
| 7053 | ts_end = Timestamp(end) |
| 7054 | except (ValueError, TypeError): |
| 7055 | pass |
| 7056 | else: |
| 7057 | if not tz_compare(ts_start.tzinfo, ts_end.tzinfo): |