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

Method _join_multi

pandas/core/indexes/base.py:4585–4671  ·  view source on GitHub ↗
(self, other: Index, how: JoinHow)

Source from the content-addressed store, hash-verified

4583
4584 @final
4585 def _join_multi(self, other: Index, how: JoinHow):
4586 from pandas.core.indexes.multi import MultiIndex
4587 from pandas.core.reshape.merge import restore_dropped_levels_multijoin
4588
4589 # figure out join names
4590 self_names_list = list(self.names)
4591 other_names_list = list(other.names)
4592 self_names_order = self_names_list.index
4593 other_names_order = other_names_list.index
4594 self_names = set(self_names_list)
4595 other_names = set(other_names_list)
4596 overlap = self_names & other_names
4597
4598 # need at least 1 in common
4599 if not overlap:
4600 raise ValueError("cannot join with no overlapping index names")
4601
4602 if isinstance(self, MultiIndex) and isinstance(other, MultiIndex):
4603 # Drop the non-matching levels from left and right respectively
4604 ldrop_names = sorted(self_names - overlap, key=self_names_order)
4605 rdrop_names = sorted(other_names - overlap, key=other_names_order)
4606
4607 # if only the order differs
4608 if not len(ldrop_names + rdrop_names):
4609 self_jnlevels = self
4610 other_jnlevels = other.reorder_levels(self.names)
4611 else:
4612 self_jnlevels = self.droplevel(ldrop_names)
4613 other_jnlevels = other.droplevel(rdrop_names)
4614
4615 # Join left and right
4616 # Join on same leveled multi-index frames is supported
4617 join_idx, lidx, ridx = self_jnlevels.join(
4618 other_jnlevels, how=how, return_indexers=True
4619 )
4620
4621 # Restore the dropped levels
4622 # Returned index level order is
4623 # common levels, ldrop_names, rdrop_names
4624 dropped_names = ldrop_names + rdrop_names
4625
4626 # error: Argument 5/6 to "restore_dropped_levels_multijoin" has
4627 # incompatible type "Optional[ndarray[Any, dtype[signedinteger[Any
4628 # ]]]]"; expected "ndarray[Any, dtype[signedinteger[Any]]]"
4629 levels, codes, names = restore_dropped_levels_multijoin(
4630 self,
4631 other,
4632 dropped_names,
4633 join_idx,
4634 lidx, # type: ignore[arg-type]
4635 ridx, # type: ignore[arg-type]
4636 )
4637
4638 # Re-create the multi-index
4639 multi_join_idx = MultiIndex(
4640 levels=levels, codes=codes, names=names, verify_integrity=False
4641 )
4642

Callers 1

joinMethod · 0.95

Calls 9

reorder_levelsMethod · 0.95
droplevelMethod · 0.95
remove_unused_levelsMethod · 0.95
_join_levelMethod · 0.95
MultiIndexClass · 0.90
joinMethod · 0.45
getMethod · 0.45
indexMethod · 0.45

Tested by

no test coverage detected