Skip to content

REF: avoid need for get_join_target in join_non_unique #43217

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Sep 2, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 31 additions & 16 deletions pandas/core/indexes/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -4092,8 +4092,6 @@ def join(
join_index, (left_indexer, right_indexer)
"""
other = ensure_index(other)
self_is_mi = isinstance(self, ABCMultiIndex)
other_is_mi = isinstance(other, ABCMultiIndex)

if isinstance(self, ABCDatetimeIndex) and isinstance(other, ABCDatetimeIndex):
if (self.tz is None) ^ (other.tz is None):
Expand All @@ -4113,7 +4111,7 @@ def join(

# try to figure out the join level
# GH3662
if level is None and (self_is_mi or other_is_mi):
if level is None and (self._is_multi or other._is_multi):

# have the same levels/names so a simple join
if self.names == other.names:
Expand All @@ -4122,7 +4120,7 @@ def join(
return self._join_multi(other, how=how)

# join on the level
if level is not None and (self_is_mi or other_is_mi):
if level is not None and (self._is_multi or other._is_multi):
return self._join_level(other, level, how=how)

if len(other) == 0 and how in ("left", "outer"):
Expand Down Expand Up @@ -4171,8 +4169,20 @@ def join(
try:
return self._join_monotonic(other, how=how)
except TypeError:
# object dtype; non-comparable objects
pass

return self._join_via_get_indexer(other, how, sort)

@final
def _join_via_get_indexer(
self, other: Index, how: str_t, sort: bool
) -> tuple[Index, npt.NDArray[np.intp] | None, npt.NDArray[np.intp] | None]:
# Fallback if we do not have any fastpaths available based on
# uniqueness/monotonicity

# Note: at this point we have checked matching dtypes

if how == "left":
join_index = self
elif how == "right":
Expand Down Expand Up @@ -4282,22 +4292,25 @@ def _join_non_unique(
# We only get here if dtypes match
assert self.dtype == other.dtype

lvalues = self._get_join_target()
rvalues = other._get_join_target()

left_idx, right_idx = get_join_indexers(
[lvalues], [rvalues], how=how, sort=True
[self._values], [other._values], how=how, sort=True
)
mask = left_idx == -1

left_idx = ensure_platform_int(left_idx)
right_idx = ensure_platform_int(right_idx)
# error: Argument 1 to "take" of "ExtensionArray" has incompatible
# type "ndarray[Any, dtype[signedinteger[Any]]]"; expected "Sequence[int]"
join_array = self._values.take(left_idx) # type: ignore[arg-type]
# error: Argument 1 to "take" of "ExtensionArray" has incompatible type
# "ndarray[Any, dtype[signedinteger[Any]]]"; expected "Sequence[int]"
right = other._values.take(right_idx) # type: ignore[arg-type]

join_array = np.asarray(lvalues.take(left_idx))
mask = left_idx == -1
np.putmask(join_array, mask, rvalues.take(right_idx))
if isinstance(join_array, np.ndarray):
np.putmask(join_array, mask, right)
else:
# error: "ExtensionArray" has no attribute "putmask"
join_array.putmask(mask, right) # type: ignore[attr-defined]

join_arraylike = self._from_join_target(join_array)
join_index = self._wrap_joined_index(join_arraylike, other)
join_index = self._wrap_joined_index(join_array, other)

return join_index, left_idx, right_idx

Expand Down Expand Up @@ -4450,7 +4463,9 @@ def _get_leaf_sorter(labels: list[np.ndarray]) -> npt.NDArray[np.intp]:
return join_index, left_indexer, right_indexer

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

Expand Down