Skip to content

Commit ae57438

Browse files
committed
DOC: .get_slice_bound in MultiIndex needs documentation. (#29967)
1 parent 2043969 commit ae57438

File tree

2 files changed

+49
-1
lines changed

2 files changed

+49
-1
lines changed

pandas/_typing.py

+3
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,11 @@
44
TYPE_CHECKING,
55
AnyStr,
66
Dict,
7+
Hashable,
78
Iterable,
89
List,
910
Optional,
11+
Sequence,
1012
TypeVar,
1113
Union,
1214
)
@@ -36,6 +38,7 @@
3638
Axis = Union[str, int]
3739
Ordered = Optional[bool]
3840
JSONSerializable = Union[Scalar, List, Dict]
41+
OneOrMoreObjects = Union[Hashable, Sequence[Hashable]]
3942

4043
# use Collection after we drop support for py35
4144
Axes = Iterable

pandas/core/indexes/multi.py

+46-1
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
from pandas.core.dtypes.generic import ABCDataFrame
3131
from pandas.core.dtypes.missing import array_equivalent, isna
3232

33+
from pandas._typing import OneOrMoreObjects
3334
import pandas.core.algorithms as algos
3435
from pandas.core.arrays import Categorical
3536
from pandas.core.arrays.categorical import factorize_from_iterables
@@ -2441,7 +2442,51 @@ def reindex(self, target, method=None, level=None, limit=None, tolerance=None):
24412442

24422443
return target, indexer
24432444

2444-
def get_slice_bound(self, label, side, kind):
2445+
def get_slice_bound(self, label: OneOrMoreObjects, side: str, kind: str) -> int:
2446+
"""
2447+
For an ordered MultiIndex, compute slice bound
2448+
that corresponds to given label.
2449+
2450+
Returns leftmost (one-past-the-rightmost if `side=='right') position
2451+
of given label.
2452+
2453+
Parameters
2454+
----------
2455+
label : object or tuple of objects
2456+
side : {'left', 'right'}
2457+
kind : {'ix', 'loc', 'getitem'}
2458+
2459+
Returns
2460+
-------
2461+
int
2462+
Index of label.
2463+
2464+
Notes
2465+
-----
2466+
This method only works if level 0 index of the MultiIndex is lexsorted.
2467+
2468+
Examples
2469+
--------
2470+
>>> mi = pd.MultiIndex.from_arrays([list('abbc'), list('gefd')])
2471+
2472+
Get the locations from the leftmost 'b' in the first level
2473+
until the end of the multiindex:
2474+
2475+
>>> mi.get_slice_bound('b', side="left", kind="ix")
2476+
1
2477+
2478+
Like above, but if you get the locations from the rightmost
2479+
'b' in the first level and 'f' in the second level:
2480+
2481+
>>> mi.get_slice_bound(('b','f'), side="right", kind="ix")
2482+
3
2483+
2484+
See Also
2485+
--------
2486+
MultiIndex.get_loc : Get location for a label or a tuple of labels.
2487+
MultiIndex.get_locs : Get location for a label/slice/list/mask or a
2488+
sequence of such.
2489+
"""
24452490

24462491
if not isinstance(label, tuple):
24472492
label = (label,)

0 commit comments

Comments
 (0)