Skip to content

Commit 0acdd00

Browse files
committed
Adress review comments
1 parent f573ca4 commit 0acdd00

File tree

1 file changed

+23
-18
lines changed

1 file changed

+23
-18
lines changed

pandas/core/reshape/merge.py

Lines changed: 23 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
import copy
66
import datetime
7+
import hashlib
78
from functools import partial
89
import string
910
from typing import TYPE_CHECKING, Optional, Tuple, cast
@@ -591,8 +592,6 @@ def __init__(
591592
):
592593
_left = _validate_operand(left)
593594
_right = _validate_operand(right)
594-
if how == "cross":
595-
_left, _right, how, on = self._create_cross_configuration(_left, _right, on)
596595
self.left = self.orig_left = _left
597596
self.right = self.orig_right = _right
598597
self.how = how
@@ -645,6 +644,16 @@ def __init__(
645644

646645
self._validate_specification()
647646

647+
if self.how == "cross":
648+
(
649+
self.left,
650+
self.right,
651+
self.how,
652+
cross_col,
653+
) = self._create_cross_configuration(self.left, self.right)
654+
self.left_on = self.right_on = [cross_col]
655+
self._cross = cross_col
656+
648657
# note this function has side effects
649658
(
650659
self.left_join_keys,
@@ -1210,35 +1219,31 @@ def _maybe_coerce_merge_keys(self):
12101219
self.right = self.right.assign(**{name: self.right[name].astype(typ)})
12111220

12121221
def _create_cross_configuration(
1213-
self, _left, _right, on
1222+
self, _left, _right
12141223
) -> Tuple["DataFrame", "DataFrame", str, str]:
1215-
if on is not None:
1216-
raise MergeError(
1217-
"Can not pass any merge columns when using cross as merge method"
1218-
)
1219-
cross_col = f"{max([*_left.columns, *_right.columns])}_cross"
1220-
_left = _left.copy()
1221-
_right = _right.copy()
1222-
_left.insert(loc=0, value=1, column=cross_col)
1223-
_right.insert(loc=0, value=1, column=cross_col)
1224+
cross_col = f"_cross_{hashlib.md5().hexdigest()}"
12241225
how = "inner"
1225-
on = cross_col
1226-
self._cross = cross_col
1227-
return _left, _right, how, on
1226+
return (
1227+
_left.assign(**{cross_col: 1}),
1228+
_right.assign(**{cross_col: 1}),
1229+
how,
1230+
cross_col,
1231+
)
12281232

12291233
def _validate_specification(self):
1230-
if hasattr(self, "_cross"):
1234+
if self.how == "cross":
12311235
if (
12321236
self.left_index
12331237
or self.right_index
12341238
or self.right_on is not None
12351239
or self.left_on is not None
1240+
or self.on is not None
12361241
):
12371242
raise MergeError(
12381243
"Can not pass any merge columns when using cross as merge method"
12391244
)
12401245
# Hm, any way to make this logic less complicated??
1241-
if self.on is None and self.left_on is None and self.right_on is None:
1246+
elif self.on is None and self.left_on is None and self.right_on is None:
12421247

12431248
if self.left_index and self.right_index:
12441249
self.left_on, self.right_on = (), ()
@@ -1302,7 +1307,7 @@ def _validate_specification(self):
13021307
'of levels in the index of "left"'
13031308
)
13041309
self.left_on = [None] * n
1305-
if len(self.right_on) != len(self.left_on):
1310+
if self.how != "cross" and len(self.right_on) != len(self.left_on):
13061311
raise ValueError("len(right_on) must equal len(left_on)")
13071312

13081313
def _validate(self, validate: str):

0 commit comments

Comments
 (0)