From c25f459891014ec6938d6e8554ae56c99b0c186f Mon Sep 17 00:00:00 2001 From: HH Date: Fri, 13 Dec 2019 22:54:37 +0900 Subject: [PATCH] DOC: .get_slice_bound in MultiIndex needs documentation. (#29967) --- pandas/_typing.py | 3 +++ pandas/core/indexes/multi.py | 48 +++++++++++++++++++++++++++++++++++- 2 files changed, 50 insertions(+), 1 deletion(-) diff --git a/pandas/_typing.py b/pandas/_typing.py index 445eff9e19e479..7e586dedc3d9d1 100644 --- a/pandas/_typing.py +++ b/pandas/_typing.py @@ -4,9 +4,11 @@ TYPE_CHECKING, AnyStr, Dict, + Hashable, Iterable, List, Optional, + Sequence, TypeVar, Union, ) @@ -36,6 +38,7 @@ Axis = Union[str, int] Ordered = Optional[bool] JSONSerializable = Union[Scalar, List, Dict] +OneOrMoreObjects = Union[Hashable, Sequence[Hashable]] # use Collection after we drop support for py35 Axes = Iterable diff --git a/pandas/core/indexes/multi.py b/pandas/core/indexes/multi.py index 57c7cd2df81c15..f420ac30f9b836 100644 --- a/pandas/core/indexes/multi.py +++ b/pandas/core/indexes/multi.py @@ -56,6 +56,8 @@ pprint_thing, ) +from pandas._typing import OneOrMoreObjects + _index_doc_kwargs = dict(ibase._index_doc_kwargs) _index_doc_kwargs.update( 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): return target, indexer - def get_slice_bound(self, label, side, kind): + def get_slice_bound(self, label: OneOrMoreObjects, side: str, kind: str) -> int: + """ + For an ordered MultiIndex, compute slice bound + that corresponds to given label. + + Returns leftmost (one-past-the-rightmost if `side=='right') position + of given label. + + Parameters + ---------- + label : object or tuple of objects + side : {'left', 'right'} + kind : {'ix', 'loc', 'getitem'} + + Returns + ------- + int + Index of label. + + Notes + ----- + This method only works if level 0 index of the MultiIndex is lexsorted. + + Examples + -------- + >>> mi = pd.MultiIndex.from_arrays([list('abbc'), list('gefd')]) + + Get the locations from the leftmost 'b' in the first level + until the end of the multiindex: + + >>> mi.get_slice_bound('b', side="left", kind="ix") + 1 + + Like above, but if you get the locations from the rightmost + 'b' in the first level and 'f' in the second level: + + >>> mi.get_slice_bound(('b','f'), side="right", kind="ix") + 3 + + See Also + -------- + MultiIndex.get_loc : Get location for a label or a tuple of labels. + MultiIndex.get_locs : Get location for a label/slice/list/mask or a + sequence of such. + """ if not isinstance(label, tuple): label = (label,)