Skip to content

Commit c25f459

Browse files
committed
DOC: .get_slice_bound in MultiIndex needs documentation. (pandas-dev#29967)
1 parent 2043969 commit c25f459

File tree

2 files changed

+50
-1
lines changed

2 files changed

+50
-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

+47-1
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,8 @@
5656
pprint_thing,
5757
)
5858

59+
from pandas._typing import OneOrMoreObjects
60+
5961
_index_doc_kwargs = dict(ibase._index_doc_kwargs)
6062
_index_doc_kwargs.update(
6163
dict(klass="MultiIndex", target_klass="MultiIndex or list of tuples")
@@ -2441,7 +2443,51 @@ def reindex(self, target, method=None, level=None, limit=None, tolerance=None):
24412443

24422444
return target, indexer
24432445

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

24462492
if not isinstance(label, tuple):
24472493
label = (label,)

0 commit comments

Comments
 (0)