Skip to content

Commit 7e8331b

Browse files
authored
REF: avoid need for get_join_target in join_non_unique (#43217)
1 parent 11c3a08 commit 7e8331b

File tree

1 file changed

+31
-16
lines changed

1 file changed

+31
-16
lines changed

pandas/core/indexes/base.py

Lines changed: 31 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -4087,8 +4087,6 @@ def join(
40874087
join_index, (left_indexer, right_indexer)
40884088
"""
40894089
other = ensure_index(other)
4090-
self_is_mi = isinstance(self, ABCMultiIndex)
4091-
other_is_mi = isinstance(other, ABCMultiIndex)
40924090

40934091
if isinstance(self, ABCDatetimeIndex) and isinstance(other, ABCDatetimeIndex):
40944092
if (self.tz is None) ^ (other.tz is None):
@@ -4108,7 +4106,7 @@ def join(
41084106

41094107
# try to figure out the join level
41104108
# GH3662
4111-
if level is None and (self_is_mi or other_is_mi):
4109+
if level is None and (self._is_multi or other._is_multi):
41124110

41134111
# have the same levels/names so a simple join
41144112
if self.names == other.names:
@@ -4117,7 +4115,7 @@ def join(
41174115
return self._join_multi(other, how=how)
41184116

41194117
# join on the level
4120-
if level is not None and (self_is_mi or other_is_mi):
4118+
if level is not None and (self._is_multi or other._is_multi):
41214119
return self._join_level(other, level, how=how)
41224120

41234121
if len(other) == 0 and how in ("left", "outer"):
@@ -4166,8 +4164,20 @@ def join(
41664164
try:
41674165
return self._join_monotonic(other, how=how)
41684166
except TypeError:
4167+
# object dtype; non-comparable objects
41694168
pass
41704169

4170+
return self._join_via_get_indexer(other, how, sort)
4171+
4172+
@final
4173+
def _join_via_get_indexer(
4174+
self, other: Index, how: str_t, sort: bool
4175+
) -> tuple[Index, npt.NDArray[np.intp] | None, npt.NDArray[np.intp] | None]:
4176+
# Fallback if we do not have any fastpaths available based on
4177+
# uniqueness/monotonicity
4178+
4179+
# Note: at this point we have checked matching dtypes
4180+
41714181
if how == "left":
41724182
join_index = self
41734183
elif how == "right":
@@ -4277,22 +4287,25 @@ def _join_non_unique(
42774287
# We only get here if dtypes match
42784288
assert self.dtype == other.dtype
42794289

4280-
lvalues = self._get_join_target()
4281-
rvalues = other._get_join_target()
4282-
42834290
left_idx, right_idx = get_join_indexers(
4284-
[lvalues], [rvalues], how=how, sort=True
4291+
[self._values], [other._values], how=how, sort=True
42854292
)
4293+
mask = left_idx == -1
42864294

4287-
left_idx = ensure_platform_int(left_idx)
4288-
right_idx = ensure_platform_int(right_idx)
4295+
# error: Argument 1 to "take" of "ExtensionArray" has incompatible
4296+
# type "ndarray[Any, dtype[signedinteger[Any]]]"; expected "Sequence[int]"
4297+
join_array = self._values.take(left_idx) # type: ignore[arg-type]
4298+
# error: Argument 1 to "take" of "ExtensionArray" has incompatible type
4299+
# "ndarray[Any, dtype[signedinteger[Any]]]"; expected "Sequence[int]"
4300+
right = other._values.take(right_idx) # type: ignore[arg-type]
42894301

4290-
join_array = np.asarray(lvalues.take(left_idx))
4291-
mask = left_idx == -1
4292-
np.putmask(join_array, mask, rvalues.take(right_idx))
4302+
if isinstance(join_array, np.ndarray):
4303+
np.putmask(join_array, mask, right)
4304+
else:
4305+
# error: "ExtensionArray" has no attribute "putmask"
4306+
join_array.putmask(mask, right) # type: ignore[attr-defined]
42934307

4294-
join_arraylike = self._from_join_target(join_array)
4295-
join_index = self._wrap_joined_index(join_arraylike, other)
4308+
join_index = self._wrap_joined_index(join_array, other)
42964309

42974310
return join_index, left_idx, right_idx
42984311

@@ -4445,7 +4458,9 @@ def _get_leaf_sorter(labels: list[np.ndarray]) -> npt.NDArray[np.intp]:
44454458
return join_index, left_indexer, right_indexer
44464459

44474460
@final
4448-
def _join_monotonic(self, other: Index, how: str_t = "left"):
4461+
def _join_monotonic(
4462+
self, other: Index, how: str_t = "left"
4463+
) -> tuple[Index, npt.NDArray[np.intp] | None, npt.NDArray[np.intp] | None]:
44494464
# We only get here with matching dtypes
44504465
assert other.dtype == self.dtype
44514466

0 commit comments

Comments
 (0)