-
-
Notifications
You must be signed in to change notification settings - Fork 18.4k
ENH: Improve error reporting for wrong merge cols #37547
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
Changes from 5 commits
87835fa
f6d9776
490062b
8f787d3
e865ae7
3981fb6
152e70c
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2283,3 +2283,57 @@ def test_merge_join_categorical_multiindex(): | |
expected = expected.drop(["Cat", "Int"], axis=1) | ||
result = a.join(b, on=["Cat1", "Int1"]) | ||
tm.assert_frame_equal(expected, result) | ||
|
||
|
||
@pytest.mark.parametrize("func", ["merge", "merge_asof"]) | ||
@pytest.mark.parametrize( | ||
("kwargs", "err_msg"), | ||
[ | ||
({"left_on": "a", "left_index": True}, ["left_on", "left_index"]), | ||
({"right_on": "a", "right_index": True}, ["right_on", "right_index"]), | ||
], | ||
) | ||
def test_merge_join_cols_error_reporting_duplicates(func, kwargs, err_msg): | ||
# GH: 16228 | ||
left = DataFrame({"a": [1, 2], "b": [3, 4]}) | ||
right = DataFrame({"a": [1, 1], "c": [5, 6]}) | ||
msg = rf"Can only pass argument \"{err_msg[0]}\" OR \"{err_msg[1]}\" not both\." | ||
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. This would be more readable in single quotes. 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. thx |
||
with pytest.raises(MergeError, match=msg): | ||
getattr(pd, func)(left, right, **kwargs) | ||
|
||
|
||
@pytest.mark.parametrize("func", ["merge", "merge_asof"]) | ||
@pytest.mark.parametrize( | ||
("kwargs", "err_msg"), | ||
[ | ||
({"left_on": "a"}, ["right_on", "right_index"]), | ||
({"right_on": "a"}, ["left_on", "left_index"]), | ||
], | ||
) | ||
def test_merge_join_cols_error_reporting_missing(func, kwargs, err_msg): | ||
# GH: 16228 | ||
left = DataFrame({"a": [1, 2], "b": [3, 4]}) | ||
right = DataFrame({"a": [1, 1], "c": [5, 6]}) | ||
msg = rf"Must pass \"{err_msg[0]}\" OR \"{err_msg[1]}\"\." | ||
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. Same in regards to quotes. 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. thx |
||
with pytest.raises(MergeError, match=msg): | ||
getattr(pd, func)(left, right, **kwargs) | ||
|
||
|
||
@pytest.mark.parametrize("func", ["merge", "merge_asof"]) | ||
@pytest.mark.parametrize( | ||
"kwargs", | ||
[ | ||
{"right_index": True}, | ||
{"left_index": True}, | ||
], | ||
) | ||
def test_merge_join_cols_error_reporting_on_and_index(func, kwargs): | ||
# GH: 16228 | ||
left = DataFrame({"a": [1, 2], "b": [3, 4]}) | ||
right = DataFrame({"a": [1, 1], "c": [5, 6]}) | ||
msg = ( | ||
r"Can only pass argument \"on\" OR \"left_index\" " | ||
r"and \"right_index\", not a combination of both\." | ||
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. This would be more readable in single quotes. Or are they rejected by black? 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. Thx. That is a good point. Changed it |
||
) | ||
with pytest.raises(MergeError, match=msg): | ||
getattr(pd, func)(left, right, on="a", **kwargs) |
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.
Nice catch.