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