Skip to content

Commit 4589651

Browse files
committed
Raise if duplicate on column
1 parent 4fcde78 commit 4589651

File tree

2 files changed

+19
-0
lines changed

2 files changed

+19
-0
lines changed

pandas/core/reshape/merge.py

+5
Original file line numberDiff line numberDiff line change
@@ -1238,6 +1238,11 @@ def _create_cross_configuration(
12381238
to join over.
12391239
"""
12401240
cross_col = f"_cross_{hashlib.md5().hexdigest()}"
1241+
if cross_col in left.columns or cross_col in right.columns:
1242+
raise MergeError(
1243+
f"{cross_col} is the synthetic column to perform the "
1244+
f"cross merge. This column can not be an input column."
1245+
)
12411246
how = "inner"
12421247
return (
12431248
left.assign(**{cross_col: 1}),

pandas/tests/reshape/merge/test_merge_cross.py

+14
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import hashlib
2+
13
import pytest
24

35
from pandas import DataFrame
@@ -93,3 +95,15 @@ def test_join_cross_error_reporting():
9395
)
9496
with pytest.raises(MergeError, match=msg):
9597
left.join(right, how="cross", on="a")
98+
99+
100+
def test_merge_cross_duplicate_on_column():
101+
# GH#5401
102+
left = DataFrame({"a": [1, 2], f"_cross_{hashlib.md5().hexdigest()}": [2, 3]})
103+
right = DataFrame({"b": [3]})
104+
msg = (
105+
f"_cross_{hashlib.md5().hexdigest()} is the synthetic column to perform "
106+
f"the cross merge. This column can not be an input column."
107+
)
108+
with pytest.raises(MergeError, match=msg):
109+
merge(left, right, how="cross")

0 commit comments

Comments
 (0)