def _join_level(self, other, level, how='<STR_LIT:left>', return_indexers=False,<EOL>keep_order=True):
from .multi import MultiIndex<EOL>def _get_leaf_sorter(labels):<EOL><INDENT>"""<STR_LIT>"""<EOL>if labels[<NUM_LIT:0>].size == <NUM_LIT:0>:<EOL><INDENT>return np.empty(<NUM_LIT:0>, dtype='<STR_LIT>')<EOL><DEDENT>if len(labels) == <NUM_LIT:1>:<EOL><INDENT>lab = ensure_int64(labels[<NUM_LIT:0>])<EOL>sorter, _ = libalgos.groupsort_indexer(lab, <NUM_LIT:1> + lab.max())<EOL>return sorter<EOL><DEDENT>tic = labels[<NUM_LIT:0>][:-<NUM_LIT:1>] != labels[<NUM_LIT:0>][<NUM_LIT:1>:]<EOL>for lab in labels[<NUM_LIT:1>:-<NUM_LIT:1>]:<EOL><INDENT>tic |= lab[:-<NUM_LIT:1>] != lab[<NUM_LIT:1>:]<EOL><DEDENT>starts = np.hstack(([True], tic, [True])).nonzero()[<NUM_LIT:0>]<EOL>lab = ensure_int64(labels[-<NUM_LIT:1>])<EOL>return lib.get_level_sorter(lab, ensure_int64(starts))<EOL><DEDENT>if isinstance(self, MultiIndex) and isinstance(other, MultiIndex):<EOL><INDENT>raise TypeError('<STR_LIT>'<EOL>'<STR_LIT>')<EOL><DEDENT>left, right = self, other<EOL>flip_order = not isinstance(self, MultiIndex)<EOL>if flip_order:<EOL><INDENT>left, right = right, left<EOL>how = {'<STR_LIT:right>': '<STR_LIT:left>', '<STR_LIT:left>': '<STR_LIT:right>'}.get(how, how)<EOL><DEDENT>level = left._get_level_number(level)<EOL>old_level = left.levels[level]<EOL>if not right.is_unique:<EOL><INDENT>raise NotImplementedError('<STR_LIT>'<EOL>'<STR_LIT>')<EOL><DEDENT>new_level, left_lev_indexer, right_lev_indexer =old_level.join(right, how=how, return_indexers=True)<EOL>if left_lev_indexer is None:<EOL><INDENT>if keep_order or len(left) == <NUM_LIT:0>:<EOL><INDENT>left_indexer = None<EOL>join_index = left<EOL><DEDENT>else:  <EOL><INDENT>left_indexer = _get_leaf_sorter(left.codes[:level + <NUM_LIT:1>])<EOL>join_index = left[left_indexer]<EOL><DEDENT><DEDENT>else:<EOL><INDENT>left_lev_indexer = ensure_int64(left_lev_indexer)<EOL>rev_indexer = lib.get_reverse_indexer(left_lev_indexer,<EOL>len(old_level))<EOL>new_lev_codes = algos.take_nd(rev_indexer, left.codes[level],<EOL>allow_fill=False)<EOL>new_codes = list(left.codes)<EOL>new_codes[level] = new_lev_codes<EOL>new_levels = list(left.levels)<EOL>new_levels[level] = new_level<EOL>if keep_order:  <EOL><INDENT>left_indexer = np.arange(len(left), dtype=np.intp)<EOL>mask = new_lev_codes != -<NUM_LIT:1><EOL>if not mask.all():<EOL><INDENT>new_codes = [lab[mask] for lab in new_codes]<EOL>left_indexer = left_indexer[mask]<EOL><DEDENT><DEDENT>else:  <EOL><INDENT>if level == <NUM_LIT:0>:  <EOL><INDENT>ngroups = <NUM_LIT:1> + new_lev_codes.max()<EOL>left_indexer, counts = libalgos.groupsort_indexer(<EOL>new_lev_codes, ngroups)<EOL>left_indexer = left_indexer[counts[<NUM_LIT:0>]:]<EOL>new_codes = [lab[left_indexer] for lab in new_codes]<EOL><DEDENT>else:  <EOL><INDENT>mask = new_lev_codes != -<NUM_LIT:1><EOL>mask_all = mask.all()<EOL>if not mask_all:<EOL><INDENT>new_codes = [lab[mask] for lab in new_codes]<EOL><DEDENT>left_indexer = _get_leaf_sorter(new_codes[:level + <NUM_LIT:1>])<EOL>new_codes = [lab[left_indexer] for lab in new_codes]<EOL>if not mask_all:<EOL><INDENT>left_indexer = mask.nonzero()[<NUM_LIT:0>][left_indexer]<EOL><DEDENT><DEDENT><DEDENT>join_index = MultiIndex(levels=new_levels, codes=new_codes,<EOL>names=left.names, verify_integrity=False)<EOL><DEDENT>if right_lev_indexer is not None:<EOL><INDENT>right_indexer = algos.take_nd(right_lev_indexer,<EOL>join_index.codes[level],<EOL>allow_fill=False)<EOL><DEDENT>else:<EOL><INDENT>right_indexer = join_index.codes[level]<EOL><DEDENT>if flip_order:<EOL><INDENT>left_indexer, right_indexer = right_indexer, left_indexer<EOL><DEDENT>if return_indexers:<EOL><INDENT>left_indexer = (None if left_indexer is None<EOL>else ensure_platform_int(left_indexer))<EOL>right_indexer = (None if right_indexer is None<EOL>else ensure_platform_int(right_indexer))<EOL>return join_index, left_indexer, right_indexer<EOL><DEDENT>else:<EOL><INDENT>return join_index<EOL><DEDENT>
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 changed; otherwise, it will tie out
with `other`.