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

Method _join_level

pandas/core/indexes/base.py:4703–4848  ·  view source on GitHub ↗

The join method *only* affects the level of the resulting MultiIndex. Otherwise it just exactly aligns the Index data to the labels of the level in the MultiIndex. If ```keep_order == True```, the order of the data indexed by the MultiIndex will not be chang

(
        self, other: Index, level, how: JoinHow = "left", keep_order: bool = True
    )

Source from the content-addressed store, hash-verified

4701
4702 @final
4703 def _join_level(
4704 self, other: Index, level, how: JoinHow = "left", keep_order: bool = True
4705 ) -> tuple[MultiIndex, npt.NDArray[np.intp] | None, npt.NDArray[np.intp] | None]:
4706 """
4707 The join method *only* affects the level of the resulting
4708 MultiIndex. Otherwise it just exactly aligns the Index data to the
4709 labels of the level in the MultiIndex.
4710
4711 If ```keep_order == True```, the order of the data indexed by the
4712 MultiIndex will not be changed; otherwise, it will tie out
4713 with `other`.
4714 """
4715 from pandas.core.indexes.multi import MultiIndex
4716
4717 def _get_leaf_sorter(labels: list[np.ndarray]) -> npt.NDArray[np.intp]:
4718 """
4719 Returns sorter for the inner most level while preserving the
4720 order of higher levels.
4721
4722 Parameters
4723 ----------
4724 labels : list[np.ndarray]
4725 Each ndarray has signed integer dtype, not necessarily identical.
4726
4727 Returns
4728 -------
4729 np.ndarray[np.intp]
4730 """
4731 if labels[0].size == 0:
4732 return np.empty(0, dtype=np.intp)
4733
4734 if len(labels) == 1:
4735 return get_group_index_sorter(ensure_platform_int(labels[0]))
4736
4737 # find indexers of beginning of each set of
4738 # same-key labels w.r.t all but last level
4739 tic = labels[0][:-1] != labels[0][1:]
4740 for lab in labels[1:-1]:
4741 tic |= lab[:-1] != lab[1:]
4742
4743 starts = np.hstack(([True], tic, [True])).nonzero()[0]
4744 lab = ensure_int64(labels[-1])
4745 return lib.get_level_sorter(lab, ensure_platform_int(starts))
4746
4747 if isinstance(self, MultiIndex) and isinstance(other, MultiIndex):
4748 raise TypeError("Join on level between two MultiIndex objects is ambiguous")
4749
4750 left, right = self, other
4751
4752 flip_order = not isinstance(self, MultiIndex)
4753 if flip_order:
4754 left, right = right, left
4755 flip: dict[JoinHow, JoinHow] = {"right": "left", "left": "right"}
4756 how = flip.get(how, how)
4757
4758 assert isinstance(left, MultiIndex)
4759
4760 level = left._get_level_number(level)

Callers 3

reindexMethod · 0.95
joinMethod · 0.95
_join_multiMethod · 0.95

Calls 8

MultiIndexClass · 0.90
nonzeroMethod · 0.80
getMethod · 0.45
_get_level_numberMethod · 0.45
joinMethod · 0.45
takeMethod · 0.45
allMethod · 0.45
maxMethod · 0.45

Tested by

no test coverage detected