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

Method _reorder_indexer

pandas/core/indexes/multi.py:4052–4138  ·  view source on GitHub ↗

Reorder an indexer of a MultiIndex (self) so that the labels are in the same order as given in seq Parameters ---------- seq : label/slice/list/mask or a sequence of such indexer: a position indexer of self Returns ------- in

(
        self,
        seq: tuple[Scalar | Iterable | AnyArrayLike, ...],
        indexer: npt.NDArray[np.intp],
    )

Source from the content-addressed store, hash-verified

4050 # --------------------------------------------------------------------
4051
4052 def _reorder_indexer(
4053 self,
4054 seq: tuple[Scalar | Iterable | AnyArrayLike, ...],
4055 indexer: npt.NDArray[np.intp],
4056 ) -> npt.NDArray[np.intp]:
4057 """
4058 Reorder an indexer of a MultiIndex (self) so that the labels are in the
4059 same order as given in seq
4060
4061 Parameters
4062 ----------
4063 seq : label/slice/list/mask or a sequence of such
4064 indexer: a position indexer of self
4065
4066 Returns
4067 -------
4068 indexer : a sorted position indexer of self ordered as seq
4069 """
4070
4071 # check if sorting is necessary
4072 need_sort = False
4073 for i, k in enumerate(seq):
4074 if com.is_null_slice(k) or com.is_bool_indexer(k) or is_scalar(k):
4075 pass
4076 elif is_list_like(k):
4077 if len(k) <= 1: # type: ignore[arg-type]
4078 pass
4079 elif self._is_lexsorted():
4080 # If the index is lexsorted and the list_like label
4081 # in seq are sorted then we do not need to sort
4082 k_codes = self.levels[i].get_indexer(k)
4083 k_codes = k_codes[k_codes >= 0] # Filter absent keys
4084 # True if the given codes are not ordered
4085 need_sort = (k_codes[:-1] > k_codes[1:]).any()
4086 else:
4087 need_sort = True
4088 elif isinstance(k, slice):
4089 if self._is_lexsorted():
4090 need_sort = k.step is not None and k.step < 0
4091 else:
4092 need_sort = True
4093 else:
4094 need_sort = True
4095 if need_sort:
4096 break
4097 if not need_sort:
4098 return indexer
4099
4100 n = len(self)
4101 keys: tuple[np.ndarray, ...] = ()
4102 # For each level of the sequence in seq, map the level codes with the
4103 # order they appears in a list-like sequence
4104 # This mapping is then use to reorder the indexer
4105 for i, k in enumerate(seq):
4106 if is_scalar(k):
4107 # GH#34603 we want to treat a scalar the same as an all equal list
4108 k = [k]
4109 if com.is_bool_indexer(k):

Callers 1

get_locsMethod · 0.95

Calls 5

_is_lexsortedMethod · 0.95
sanitize_arrayFunction · 0.90
get_indexerMethod · 0.45
anyMethod · 0.45
uniqueMethod · 0.45

Tested by

no test coverage detected