MCPcopy
hub / github.com/pandas-dev/pandas / _get_level_indexer

Method _get_level_indexer

pandas/core/indexes/multi.py:3802–3913  ·  view source on GitHub ↗
(
        self, key, level: int = 0, indexer: npt.NDArray[np.bool_] | None = None
    )

Source from the content-addressed store, hash-verified

3800 return indexer, result_index
3801
3802 def _get_level_indexer(
3803 self, key, level: int = 0, indexer: npt.NDArray[np.bool_] | None = None
3804 ):
3805 # `level` kwarg is _always_ positional, never name
3806 # return a boolean array or slice showing where the key is
3807 # in the totality of values
3808 # if the indexer is provided, then use this
3809
3810 level_index = self.levels[level]
3811 level_codes = self.codes[level]
3812
3813 def convert_indexer(start, stop, step, indexer=indexer, codes=level_codes):
3814 # Compute a bool indexer to identify the positions to take.
3815 # If we have an existing indexer, we only need to examine the
3816 # subset of positions where the existing indexer is True.
3817 if indexer is not None:
3818 # we only need to look at the subset of codes where the
3819 # existing indexer equals True
3820 codes = codes[indexer]
3821
3822 if step is None or step == 1:
3823 new_indexer = (codes >= start) & (codes < stop)
3824 else:
3825 r = np.arange(start, stop, step, dtype=codes.dtype)
3826 new_indexer = algos.isin(codes, r)
3827
3828 if indexer is None:
3829 return new_indexer
3830
3831 indexer = indexer.copy()
3832 indexer[indexer] = new_indexer
3833 return indexer
3834
3835 if isinstance(key, slice):
3836 # handle a slice, returning a slice if we can
3837 # otherwise a boolean indexer
3838 step = key.step
3839 is_negative_step = step is not None and step < 0
3840
3841 try:
3842 if key.start is not None:
3843 start = level_index.get_loc(key.start)
3844 elif is_negative_step:
3845 start = len(level_index) - 1
3846 else:
3847 start = 0
3848
3849 if key.stop is not None:
3850 stop = level_index.get_loc(key.stop)
3851 elif is_negative_step:
3852 stop = 0
3853 elif isinstance(start, slice):
3854 stop = len(level_index)
3855 else:
3856 stop = len(level_index) - 1
3857 except KeyError:
3858 # we have a partial slice (like looking up a partial date
3859 # string)

Callers 3

get_locMethod · 0.95
_get_loc_levelMethod · 0.95
get_locsMethod · 0.95

Calls 6

maybe_unbox_numpy_scalarFunction · 0.90
get_locMethod · 0.45
slice_indexerMethod · 0.45
searchsortedMethod · 0.45
anyMethod · 0.45

Tested by

no test coverage detected