Skip to content

CLN/TYP: "how" parameter in merge ops #56372

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 1 commit into from
Dec 7, 2023
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
30 changes: 10 additions & 20 deletions pandas/core/reshape/merge.py
Original file line number Diff line number Diff line change
Expand Up @@ -718,7 +718,7 @@ class _MergeOperation:
"""

_merge_type = "merge"
how: MergeHow | Literal["asof"]
how: JoinHow | Literal["asof"]
on: IndexLabel | None
# left_on/right_on may be None when passed, but in validate_specification
# get replaced with non-None.
Expand All @@ -739,7 +739,7 @@ def __init__(
self,
left: DataFrame | Series,
right: DataFrame | Series,
how: MergeHow | Literal["asof"] = "inner",
how: JoinHow | Literal["asof"] = "inner",
on: IndexLabel | AnyArrayLike | None = None,
left_on: IndexLabel | AnyArrayLike | None = None,
right_on: IndexLabel | AnyArrayLike | None = None,
Expand Down Expand Up @@ -1106,6 +1106,8 @@ def _maybe_add_join_keys(

def _get_join_indexers(self) -> tuple[npt.NDArray[np.intp], npt.NDArray[np.intp]]:
"""return the join indexers"""
# make mypy happy
assert self.how != "asof"
return get_join_indexers(
self.left_join_keys, self.right_join_keys, sort=self.sort, how=self.how
)
Expand All @@ -1114,8 +1116,6 @@ def _get_join_indexers(self) -> tuple[npt.NDArray[np.intp], npt.NDArray[np.intp]
def _get_join_info(
self,
) -> tuple[Index, npt.NDArray[np.intp] | None, npt.NDArray[np.intp] | None]:
# make mypy happy
assert self.how != "cross"
left_ax = self.left.index
right_ax = self.right.index

Expand Down Expand Up @@ -1658,7 +1658,7 @@ def get_join_indexers(
left_keys: list[ArrayLike],
right_keys: list[ArrayLike],
sort: bool = False,
how: MergeHow | Literal["asof"] = "inner",
how: JoinHow = "inner",
) -> tuple[npt.NDArray[np.intp], npt.NDArray[np.intp]]:
"""

Expand All @@ -1684,12 +1684,12 @@ def get_join_indexers(
left_n = len(left_keys[0])
right_n = len(right_keys[0])
if left_n == 0:
if how in ["left", "inner", "cross"]:
if how in ["left", "inner"]:
return _get_empty_indexer()
elif not sort and how in ["right", "outer"]:
return _get_no_sort_one_missing_indexer(right_n, True)
elif right_n == 0:
if how in ["right", "inner", "cross"]:
if how in ["right", "inner"]:
return _get_empty_indexer()
elif not sort and how in ["left", "outer"]:
return _get_no_sort_one_missing_indexer(left_n, False)
Expand All @@ -1699,7 +1699,7 @@ def get_join_indexers(

# get left & right join labels and num. of levels at each location
mapped = (
_factorize_keys(left_keys[n], right_keys[n], sort=sort, how=how)
_factorize_keys(left_keys[n], right_keys[n], sort=sort)
for n in range(len(left_keys))
)
zipped = zip(*mapped)
Expand All @@ -1712,7 +1712,7 @@ def get_join_indexers(
# `count` is the num. of unique keys
# set(lkey) | set(rkey) == range(count)

lkey, rkey, count = _factorize_keys(lkey, rkey, sort=sort, how=how)
lkey, rkey, count = _factorize_keys(lkey, rkey, sort=sort)
# preserve left frame order if how == 'left' and sort == False
kwargs = {}
if how in ("inner", "left", "right"):
Expand Down Expand Up @@ -2166,7 +2166,6 @@ def _get_join_indexers(self) -> tuple[npt.NDArray[np.intp], npt.NDArray[np.intp]
left_join_keys[n],
right_join_keys[n],
sort=False,
how="left",
)
for n in range(len(left_join_keys))
]
Expand Down Expand Up @@ -2310,10 +2309,7 @@ def _left_join_on_index(


def _factorize_keys(
lk: ArrayLike,
rk: ArrayLike,
sort: bool = True,
how: MergeHow | Literal["asof"] = "inner",
lk: ArrayLike, rk: ArrayLike, sort: bool = True
) -> tuple[npt.NDArray[np.intp], npt.NDArray[np.intp], int]:
"""
Encode left and right keys as enumerated types.
Expand All @@ -2329,8 +2325,6 @@ def _factorize_keys(
sort : bool, defaults to True
If True, the encoding is done such that the unique elements in the
keys are sorted.
how : {'left', 'right', 'outer', 'inner'}, default 'inner'
Type of merge.

Returns
-------
Expand Down Expand Up @@ -2419,8 +2413,6 @@ def _factorize_keys(
)
if dc.null_count > 0:
count += 1
if how == "right":
return rlab, llab, count
return llab, rlab, count

if not isinstance(lk, BaseMaskedArray) and not (
Expand Down Expand Up @@ -2491,8 +2483,6 @@ def _factorize_keys(
np.putmask(rlab, rmask, count)
count += 1

if how == "right":
return rlab, llab, count
return llab, rlab, count


Expand Down