-
-
Notifications
You must be signed in to change notification settings - Fork 18.5k
BUG: Raise ValueError for non numerical join columns in merge_asof #34488
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 26 commits
660d2f5
cafedcc
fa1d909
34a5633
783bc60
e086864
a4001c6
aaa3480
ec3147e
129eb77
4680833
fb55c9f
72fb8b4
0103974
4cc8bdc
a6048a6
cdaa8ef
47e211c
3129353
0242f47
ef47721
109c824
3a66f05
da377c7
c0ce857
2c164c5
a00eb9c
f62a873
4782233
6ebb7a3
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 |
---|---|---|
|
@@ -1708,6 +1708,22 @@ def _validate_specification(self): | |
if self.left_by is not None and self.right_by is None: | ||
raise MergeError("missing right_by") | ||
|
||
lo_dtype = ( | ||
self.left[self.left_on[0]].dtype | ||
if not self.left_index | ||
else self.left.index.dtype | ||
) | ||
ro_dtype = ( | ||
self.right[self.right_on[0]].dtype | ||
if not self.right_index | ||
else self.right.index.dtype | ||
) | ||
if is_object_dtype(lo_dtype) or is_object_dtype(ro_dtype): | ||
raise ValueError( | ||
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 make this a MergeError (as everthing else is) 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. Done, issue mentioned ValueError, but MergeError is probably better |
||
f"Incompatible merge dtype, {repr(ro_dtype)} and " | ||
f"{repr(lo_dtype)}, both sides must have numeric dtype" | ||
) | ||
|
||
# add 'by' to our key-list so we can have it in the | ||
# output as a key | ||
if self.left_by is not None: | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -1168,12 +1168,12 @@ def test_on_float_by_int(self): | |
tm.assert_frame_equal(result, expected) | ||
|
||
def test_merge_datatype_error_raises(self): | ||
msg = r"incompatible merge keys \[0\] .*, must be the same type" | ||
msg = r"Incompatible merge dtype, .*, both sides must have numeric dtype" | ||
|
||
left = pd.DataFrame({"left_val": [1, 5, 10], "a": ["a", "b", "c"]}) | ||
right = pd.DataFrame({"right_val": [1, 2, 3, 6, 7], "a": [1, 2, 3, 6, 7]}) | ||
|
||
with pytest.raises(MergeError, match=msg): | ||
with pytest.raises(ValueError, match=msg): | ||
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. revert (see above) 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. Done |
||
merge_asof(left, right, on="a") | ||
|
||
def test_merge_datatype_categorical_error_raises(self): | ||
|
@@ -1373,3 +1373,36 @@ def test_left_index_right_index_tolerance(self): | |
tolerance=Timedelta(seconds=0.5), | ||
) | ||
tm.assert_frame_equal(result, expected) | ||
|
||
|
||
@pytest.mark.parametrize( | ||
"data", | ||
[["2019-06-01 00:09:12", "2019-06-01 00:10:29"], [1.0, "2019-06-01 00:10:29"]], | ||
) | ||
def test_merge_asof_non_numerical_dtype(data): | ||
# GH 29130 | ||
left = pd.DataFrame({"x": data}) | ||
right = pd.DataFrame({"x": data}) | ||
with pytest.raises( | ||
ValueError, | ||
match=r"Incompatible merge dtype, .*, both sides must have numeric dtype", | ||
): | ||
pd.merge_asof(left, right, on="x") | ||
|
||
|
||
def test_merge_asof_non_numerical_dtype_object(): | ||
# GH#29130 | ||
left = pd.DataFrame({"a": ["12", "13", "15"], "left_val1": ["a", "b", "c"]}) | ||
right = pd.DataFrame({"a": ["a", "b", "c"], "left_val": ["d", "e", "f"]}) | ||
with pytest.raises( | ||
ValueError, | ||
match=r"Incompatible merge dtype, .*, both sides must have numeric dtype", | ||
): | ||
pd.merge_asof( | ||
left, | ||
right, | ||
left_on="left_val1", | ||
right_on="a", | ||
left_by="a", | ||
right_by="left_val", | ||
) |
Uh oh!
There was an error while loading. Please reload this page.