|
1 | 1 | from collections import OrderedDict
|
2 | 2 | import datetime
|
3 | 3 | from sys import getsizeof
|
4 |
| -from typing import List, Optional |
| 4 | +from typing import Hashable, List, Optional, Sequence, Union |
5 | 5 | import warnings
|
6 | 6 |
|
7 | 7 | import numpy as np
|
@@ -2447,7 +2447,53 @@ def reindex(self, target, method=None, level=None, limit=None, tolerance=None):
|
2447 | 2447 |
|
2448 | 2448 | return target, indexer
|
2449 | 2449 |
|
2450 |
| - def get_slice_bound(self, label, side, kind): |
| 2450 | + def get_slice_bound( |
| 2451 | + self, label: Union[Hashable, Sequence[Hashable]], side: str, kind: str |
| 2452 | + ) -> int: |
| 2453 | + """ |
| 2454 | + For an ordered MultiIndex, compute slice bound |
| 2455 | + that corresponds to given label. |
| 2456 | +
|
| 2457 | + Returns leftmost (one-past-the-rightmost if `side=='right') position |
| 2458 | + of given label. |
| 2459 | +
|
| 2460 | + Parameters |
| 2461 | + ---------- |
| 2462 | + label : object or tuple of objects |
| 2463 | + side : {'left', 'right'} |
| 2464 | + kind : {'loc', 'getitem'} |
| 2465 | +
|
| 2466 | + Returns |
| 2467 | + ------- |
| 2468 | + int |
| 2469 | + Index of label. |
| 2470 | +
|
| 2471 | + Notes |
| 2472 | + ----- |
| 2473 | + This method only works if level 0 index of the MultiIndex is lexsorted. |
| 2474 | +
|
| 2475 | + Examples |
| 2476 | + -------- |
| 2477 | + >>> mi = pd.MultiIndex.from_arrays([list('abbc'), list('gefd')]) |
| 2478 | +
|
| 2479 | + Get the locations from the leftmost 'b' in the first level |
| 2480 | + until the end of the multiindex: |
| 2481 | +
|
| 2482 | + >>> mi.get_slice_bound('b', side="left", kind="loc") |
| 2483 | + 1 |
| 2484 | +
|
| 2485 | + Like above, but if you get the locations from the rightmost |
| 2486 | + 'b' in the first level and 'f' in the second level: |
| 2487 | +
|
| 2488 | + >>> mi.get_slice_bound(('b','f'), side="right", kind="loc") |
| 2489 | + 3 |
| 2490 | +
|
| 2491 | + See Also |
| 2492 | + -------- |
| 2493 | + MultiIndex.get_loc : Get location for a label or a tuple of labels. |
| 2494 | + MultiIndex.get_locs : Get location for a label/slice/list/mask or a |
| 2495 | + sequence of such. |
| 2496 | + """ |
2451 | 2497 |
|
2452 | 2498 | if not isinstance(label, tuple):
|
2453 | 2499 | label = (label,)
|
|
0 commit comments