Skip to content

Commit 4fcde78

Browse files
committed
Create new file
1 parent f731081 commit 4fcde78

File tree

2 files changed

+95
-90
lines changed

2 files changed

+95
-90
lines changed

pandas/tests/reshape/merge/test_merge.py

-90
Original file line numberDiff line numberDiff line change
@@ -2337,93 +2337,3 @@ def test_merge_join_cols_error_reporting_on_and_index(func, kwargs):
23372337
)
23382338
with pytest.raises(MergeError, match=msg):
23392339
getattr(pd, func)(left, right, on="a", **kwargs)
2340-
2341-
2342-
@pytest.mark.parametrize(
2343-
("input_col", "output_cols"), [("b", ["a", "b"]), ("a", ["a_x", "a_y"])]
2344-
)
2345-
def test_merge_cross(input_col, output_cols):
2346-
# GH#5401
2347-
left = DataFrame({"a": [1, 3]})
2348-
right = DataFrame({input_col: [3, 4]})
2349-
left_copy = left.copy()
2350-
right_copy = right.copy()
2351-
result = merge(left, right, how="cross")
2352-
expected = DataFrame({output_cols[0]: [1, 1, 3, 3], output_cols[1]: [3, 4, 3, 4]})
2353-
tm.assert_frame_equal(result, expected)
2354-
tm.assert_frame_equal(left, left_copy)
2355-
tm.assert_frame_equal(right, right_copy)
2356-
2357-
2358-
@pytest.mark.parametrize(
2359-
"kwargs",
2360-
[
2361-
{"left_index": True},
2362-
{"right_index": True},
2363-
{"on": "a"},
2364-
{"left_on": "a"},
2365-
{"right_on": "b"},
2366-
],
2367-
)
2368-
def test_merge_cross_error_reporting(kwargs):
2369-
# GH#5401
2370-
left = DataFrame({"a": [1, 3]})
2371-
right = DataFrame({"b": [3, 4]})
2372-
msg = (
2373-
"Can not pass on, right_on, left_on or set right_index=True or "
2374-
"left_index=True"
2375-
)
2376-
with pytest.raises(MergeError, match=msg):
2377-
merge(left, right, how="cross", **kwargs)
2378-
2379-
2380-
def test_merge_cross_mixed_dtypes():
2381-
# GH#5401
2382-
left = DataFrame(["a", "b", "c"], columns=["A"])
2383-
right = DataFrame(range(2), columns=["B"])
2384-
result = merge(left, right, how="cross")
2385-
expected = DataFrame({"A": ["a", "a", "b", "b", "c", "c"], "B": [0, 1, 0, 1, 0, 1]})
2386-
tm.assert_frame_equal(result, expected)
2387-
2388-
2389-
def test_merge_cross_more_than_one_column():
2390-
# GH#5401
2391-
left = DataFrame({"A": list("ab"), "B": [2, 1]})
2392-
right = DataFrame({"C": range(2), "D": range(4, 6)})
2393-
result = merge(left, right, how="cross")
2394-
expected = DataFrame(
2395-
{
2396-
"A": ["a", "a", "b", "b"],
2397-
"B": [2, 2, 1, 1],
2398-
"C": [0, 1, 0, 1],
2399-
"D": [4, 5, 4, 5],
2400-
}
2401-
)
2402-
tm.assert_frame_equal(result, expected)
2403-
2404-
2405-
def test_merge_cross_null_values(nulls_fixture):
2406-
# GH#5401
2407-
left = DataFrame({"a": [1, nulls_fixture]})
2408-
right = DataFrame({"b": ["a", "b"], "c": [1.0, 2.0]})
2409-
result = merge(left, right, how="cross")
2410-
expected = DataFrame(
2411-
{
2412-
"a": [1, 1, nulls_fixture, nulls_fixture],
2413-
"b": ["a", "b", "a", "b"],
2414-
"c": [1.0, 2.0, 1.0, 2.0],
2415-
}
2416-
)
2417-
tm.assert_frame_equal(result, expected)
2418-
2419-
2420-
def test_join_cross_error_reporting():
2421-
# GH#5401
2422-
left = DataFrame({"a": [1, 3]})
2423-
right = DataFrame({"a": [3, 4]})
2424-
msg = (
2425-
"Can not pass on, right_on, left_on or set right_index=True or "
2426-
"left_index=True"
2427-
)
2428-
with pytest.raises(MergeError, match=msg):
2429-
left.join(right, how="cross", on="a")
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
import pytest
2+
3+
from pandas import DataFrame
4+
import pandas._testing as tm
5+
from pandas.core.reshape.merge import MergeError, merge
6+
7+
8+
@pytest.mark.parametrize(
9+
("input_col", "output_cols"), [("b", ["a", "b"]), ("a", ["a_x", "a_y"])]
10+
)
11+
def test_merge_cross(input_col, output_cols):
12+
# GH#5401
13+
left = DataFrame({"a": [1, 3]})
14+
right = DataFrame({input_col: [3, 4]})
15+
left_copy = left.copy()
16+
right_copy = right.copy()
17+
result = merge(left, right, how="cross")
18+
expected = DataFrame({output_cols[0]: [1, 1, 3, 3], output_cols[1]: [3, 4, 3, 4]})
19+
tm.assert_frame_equal(result, expected)
20+
tm.assert_frame_equal(left, left_copy)
21+
tm.assert_frame_equal(right, right_copy)
22+
23+
24+
@pytest.mark.parametrize(
25+
"kwargs",
26+
[
27+
{"left_index": True},
28+
{"right_index": True},
29+
{"on": "a"},
30+
{"left_on": "a"},
31+
{"right_on": "b"},
32+
],
33+
)
34+
def test_merge_cross_error_reporting(kwargs):
35+
# GH#5401
36+
left = DataFrame({"a": [1, 3]})
37+
right = DataFrame({"b": [3, 4]})
38+
msg = (
39+
"Can not pass on, right_on, left_on or set right_index=True or "
40+
"left_index=True"
41+
)
42+
with pytest.raises(MergeError, match=msg):
43+
merge(left, right, how="cross", **kwargs)
44+
45+
46+
def test_merge_cross_mixed_dtypes():
47+
# GH#5401
48+
left = DataFrame(["a", "b", "c"], columns=["A"])
49+
right = DataFrame(range(2), columns=["B"])
50+
result = merge(left, right, how="cross")
51+
expected = DataFrame({"A": ["a", "a", "b", "b", "c", "c"], "B": [0, 1, 0, 1, 0, 1]})
52+
tm.assert_frame_equal(result, expected)
53+
54+
55+
def test_merge_cross_more_than_one_column():
56+
# GH#5401
57+
left = DataFrame({"A": list("ab"), "B": [2, 1]})
58+
right = DataFrame({"C": range(2), "D": range(4, 6)})
59+
result = merge(left, right, how="cross")
60+
expected = DataFrame(
61+
{
62+
"A": ["a", "a", "b", "b"],
63+
"B": [2, 2, 1, 1],
64+
"C": [0, 1, 0, 1],
65+
"D": [4, 5, 4, 5],
66+
}
67+
)
68+
tm.assert_frame_equal(result, expected)
69+
70+
71+
def test_merge_cross_null_values(nulls_fixture):
72+
# GH#5401
73+
left = DataFrame({"a": [1, nulls_fixture]})
74+
right = DataFrame({"b": ["a", "b"], "c": [1.0, 2.0]})
75+
result = merge(left, right, how="cross")
76+
expected = DataFrame(
77+
{
78+
"a": [1, 1, nulls_fixture, nulls_fixture],
79+
"b": ["a", "b", "a", "b"],
80+
"c": [1.0, 2.0, 1.0, 2.0],
81+
}
82+
)
83+
tm.assert_frame_equal(result, expected)
84+
85+
86+
def test_join_cross_error_reporting():
87+
# GH#5401
88+
left = DataFrame({"a": [1, 3]})
89+
right = DataFrame({"a": [3, 4]})
90+
msg = (
91+
"Can not pass on, right_on, left_on or set right_index=True or "
92+
"left_index=True"
93+
)
94+
with pytest.raises(MergeError, match=msg):
95+
left.join(right, how="cross", on="a")

0 commit comments

Comments
 (0)