-
-
Notifications
You must be signed in to change notification settings - Fork 18.4k
ENH: Implement cross method for Merge Operations #37864
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
Changes from 27 commits
Commits
Show all changes
30 commits
Select commit
Hold shift + click to select a range
71edcce
First cross merge draft for merge operation
phofl cc5d779
Merge branch 'master' of https://github.com/pandas-dev/pandas
phofl f573ca4
Fix variable assignment
phofl 0acdd00
Adress review comments
phofl 949185e
Change function signature
phofl 2d5ccaa
Add cross functionality for join
phofl 9098852
Change docs
phofl 60f6b25
Add asvs
phofl 0601243
Move import
phofl c46eab3
Assign value
phofl 6274120
Reduce asvs
phofl 891785d
Merge branch 'master' of https://github.com/pandas-dev/pandas into 5401
phofl 1f0a1c8
Remove whitespaces
phofl d7c1156
Move import
phofl 7c8d37a
Adress review
phofl 651540e
Fix doc checks
phofl f7cdd4d
Add docstring
phofl a4d24a9
Change example
phofl 741b4b7
Fix typos and rename variables
phofl 0ff78fc
Check unmodified inputs
phofl 94316f3
Add examples
phofl 94b1367
Add tests
phofl 77a9e23
Fix doc
phofl 4597642
Change signature
phofl 67a67a6
Move test
phofl f731081
Delete import
phofl 4fcde78
Create new file
phofl 4589651
Raise if duplicate on column
phofl d964ef1
Revert "Raise if duplicate on column"
phofl a1eeaa4
Merge branch 'master' of https://github.com/pandas-dev/pandas into 5401
phofl File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -803,3 +803,15 @@ def test_join_inner_multiindex_deterministic_order(): | |
index=MultiIndex.from_tuples([(2, 1, 4, 3)], names=("b", "a", "d", "c")), | ||
) | ||
tm.assert_frame_equal(result, expected) | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. can you moe to test_cross_merge (same dir) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If I understand you correctly, it is directly below the other cross tests |
||
|
||
@pytest.mark.parametrize( | ||
("input_col", "output_cols"), [("b", ["a", "b"]), ("a", ["a_x", "a_y"])] | ||
) | ||
def test_join_cross(input_col, output_cols): | ||
# GH#5401 | ||
left = DataFrame({"a": [1, 3]}) | ||
right = DataFrame({input_col: [3, 4]}) | ||
result = left.join(right, how="cross", lsuffix="_x", rsuffix="_y") | ||
expected = DataFrame({output_cols[0]: [1, 1, 3, 3], output_cols[1]: [3, 4, 3, 4]}) | ||
tm.assert_frame_equal(result, expected) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
import pytest | ||
|
||
from pandas import DataFrame | ||
import pandas._testing as tm | ||
from pandas.core.reshape.merge import MergeError, merge | ||
|
||
|
||
@pytest.mark.parametrize( | ||
("input_col", "output_cols"), [("b", ["a", "b"]), ("a", ["a_x", "a_y"])] | ||
) | ||
def test_merge_cross(input_col, output_cols): | ||
# GH#5401 | ||
left = DataFrame({"a": [1, 3]}) | ||
right = DataFrame({input_col: [3, 4]}) | ||
left_copy = left.copy() | ||
right_copy = right.copy() | ||
result = merge(left, right, how="cross") | ||
expected = DataFrame({output_cols[0]: [1, 1, 3, 3], output_cols[1]: [3, 4, 3, 4]}) | ||
tm.assert_frame_equal(result, expected) | ||
tm.assert_frame_equal(left, left_copy) | ||
tm.assert_frame_equal(right, right_copy) | ||
|
||
|
||
@pytest.mark.parametrize( | ||
"kwargs", | ||
[ | ||
{"left_index": True}, | ||
{"right_index": True}, | ||
{"on": "a"}, | ||
{"left_on": "a"}, | ||
{"right_on": "b"}, | ||
], | ||
) | ||
def test_merge_cross_error_reporting(kwargs): | ||
# GH#5401 | ||
left = DataFrame({"a": [1, 3]}) | ||
right = DataFrame({"b": [3, 4]}) | ||
msg = ( | ||
"Can not pass on, right_on, left_on or set right_index=True or " | ||
"left_index=True" | ||
) | ||
with pytest.raises(MergeError, match=msg): | ||
merge(left, right, how="cross", **kwargs) | ||
|
||
|
||
def test_merge_cross_mixed_dtypes(): | ||
# GH#5401 | ||
left = DataFrame(["a", "b", "c"], columns=["A"]) | ||
right = DataFrame(range(2), columns=["B"]) | ||
result = merge(left, right, how="cross") | ||
expected = DataFrame({"A": ["a", "a", "b", "b", "c", "c"], "B": [0, 1, 0, 1, 0, 1]}) | ||
tm.assert_frame_equal(result, expected) | ||
|
||
|
||
def test_merge_cross_more_than_one_column(): | ||
# GH#5401 | ||
left = DataFrame({"A": list("ab"), "B": [2, 1]}) | ||
right = DataFrame({"C": range(2), "D": range(4, 6)}) | ||
result = merge(left, right, how="cross") | ||
expected = DataFrame( | ||
{ | ||
"A": ["a", "a", "b", "b"], | ||
"B": [2, 2, 1, 1], | ||
"C": [0, 1, 0, 1], | ||
"D": [4, 5, 4, 5], | ||
} | ||
) | ||
tm.assert_frame_equal(result, expected) | ||
|
||
|
||
def test_merge_cross_null_values(nulls_fixture): | ||
# GH#5401 | ||
left = DataFrame({"a": [1, nulls_fixture]}) | ||
right = DataFrame({"b": ["a", "b"], "c": [1.0, 2.0]}) | ||
result = merge(left, right, how="cross") | ||
expected = DataFrame( | ||
{ | ||
"a": [1, 1, nulls_fixture, nulls_fixture], | ||
"b": ["a", "b", "a", "b"], | ||
"c": [1.0, 2.0, 1.0, 2.0], | ||
} | ||
) | ||
tm.assert_frame_equal(result, expected) | ||
|
||
|
||
def test_join_cross_error_reporting(): | ||
# GH#5401 | ||
left = DataFrame({"a": [1, 3]}) | ||
right = DataFrame({"a": [3, 4]}) | ||
msg = ( | ||
"Can not pass on, right_on, left_on or set right_index=True or " | ||
"left_index=True" | ||
) | ||
with pytest.raises(MergeError, match=msg): | ||
left.join(right, how="cross", on="a") |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
can you add an example of an inner and left merge here (and put them right before this)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done, could you please check if this is similar to what you have in mind?