Skip to content

Commit aa83319

Browse files
committed
Restructure a bit
1 parent 1723d70 commit aa83319

File tree

2 files changed

+35
-24
lines changed

2 files changed

+35
-24
lines changed

pandas/core/reshape/merge.py

Lines changed: 31 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -938,7 +938,7 @@ class _MergeOperation:
938938
"""
939939

940940
_merge_type = "merge"
941-
how: JoinHow | Literal["left_anti", "right_anti", "asof"]
941+
how: JoinHow | Literal["asof"]
942942
on: IndexLabel | None
943943
# left_on/right_on may be None when passed, but in validate_specification
944944
# get replaced with non-None.
@@ -973,8 +973,7 @@ def __init__(
973973
_right = _validate_operand(right)
974974
self.left = self.orig_left = _left
975975
self.right = self.orig_right = _right
976-
self.how = how
977-
self.anti_join = False
976+
self.how, self.anti_join = self._validate_how(how)
978977

979978
self.on = com.maybe_make_list(on)
980979

@@ -1004,26 +1003,6 @@ def __init__(
10041003
)
10051004
raise MergeError(msg)
10061005

1007-
# GH 59435: raise when "how" is not a valid Merge type
1008-
merge_type = {
1009-
"left",
1010-
"right",
1011-
"inner",
1012-
"outer",
1013-
"left_anti",
1014-
"right_anti",
1015-
"cross",
1016-
"asof",
1017-
}
1018-
if how not in merge_type:
1019-
raise ValueError(
1020-
f"'{how}' is not a valid Merge type: "
1021-
f"left, right, inner, outer, cross, asof"
1022-
)
1023-
if self.how in {"left_anti", "right_anti"}:
1024-
self.how = self.how.split("_")[0] # type: ignore[assignment]
1025-
self.anti_join = True
1026-
10271006
self.left_on, self.right_on = self._validate_left_right_on(left_on, right_on)
10281007

10291008
(
@@ -1053,6 +1032,35 @@ def __init__(
10531032
if validate is not None:
10541033
self._validate_validate_kwd(validate)
10551034

1035+
def _validate_how(
1036+
self, how: JoinHow | Literal["left_anti", "right_anti", "asof"]
1037+
) -> tuple[JoinHow, bool]:
1038+
"""
1039+
Validate the 'how' parameter and return the actual join type and whether
1040+
this is an anti join.
1041+
"""
1042+
# GH 59435: raise when "how" is not a valid Merge type
1043+
merge_type = {
1044+
"left",
1045+
"right",
1046+
"inner",
1047+
"outer",
1048+
"left_anti",
1049+
"right_anti",
1050+
"cross",
1051+
"asof",
1052+
}
1053+
if how not in merge_type:
1054+
raise ValueError(
1055+
f"'{how}' is not a valid Merge type: "
1056+
f"left, right, inner, outer, left_anti, right_anti, cross, asof"
1057+
)
1058+
anti_join = False
1059+
if how in {"left_anti", "right_anti"}:
1060+
how = how.split("_")[0] # type: ignore[assignment]
1061+
anti_join = True
1062+
return how, anti_join
1063+
10561064
def _maybe_require_matching_dtypes(
10571065
self, left_join_keys: list[ArrayLike], right_join_keys: list[ArrayLike]
10581066
) -> None:

pandas/tests/reshape/merge/test_merge.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1464,7 +1464,10 @@ def test_merge_how_validation(self):
14641464
data2 = DataFrame(
14651465
np.arange(20).reshape((5, 4)) + 1, columns=["a", "b", "x", "y"]
14661466
)
1467-
msg = "'full' is not a valid Merge type: left, right, inner, outer, cross, asof"
1467+
msg = (
1468+
"'full' is not a valid Merge type: left, right, inner, outer, "
1469+
"left_anti, right_anti, cross, asof"
1470+
)
14681471
with pytest.raises(ValueError, match=re.escape(msg)):
14691472
data1.merge(data2, how="full")
14701473

0 commit comments

Comments
 (0)