Skip to content

BUG: Added raising when merging datetime columns with timedelta columns #56613

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 11 commits into from
Dec 28, 2023
Merged
1 change: 1 addition & 0 deletions doc/source/whatsnew/v2.2.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -822,6 +822,7 @@ Reshaping
- Bug in :func:`merge_asof` raising ``TypeError`` when ``by`` dtype is not ``object``, ``int64``, or ``uint64`` (:issue:`22794`)
- Bug in :func:`merge_asof` raising incorrect error for string dtype (:issue:`56444`)
- Bug in :func:`merge_asof` when using a :class:`Timedelta` tolerance on a :class:`ArrowDtype` column (:issue:`56486`)
- Bug in :func:`merge` not raising when merging datetime columns with timedelta columns (:issue:`56455`)
- Bug in :func:`merge` not raising when merging string columns with numeric columns (:issue:`56441`)
- Bug in :func:`merge` returning columns in incorrect order when left and/or right is empty (:issue:`51929`)
- Bug in :meth:`DataFrame.melt` where an exception was raised if ``var_name`` was not a string (:issue:`55948`)
Expand Down
5 changes: 5 additions & 0 deletions pandas/core/reshape/merge.py
Original file line number Diff line number Diff line change
Expand Up @@ -1526,6 +1526,11 @@ def _maybe_coerce_merge_keys(self) -> None:
) or (lk.dtype.kind == "M" and rk.dtype.kind == "M"):
# allows datetime with different resolutions
continue
# datetime and timedelta not allowed
elif lk.dtype.kind == "M" and rk.dtype.kind == "m":
raise ValueError(msg)
elif lk.dtype.kind == "m" and rk.dtype.kind == "M":
raise ValueError(msg)

elif is_object_dtype(lk.dtype) and is_object_dtype(rk.dtype):
continue
Expand Down
20 changes: 20 additions & 0 deletions pandas/tests/reshape/merge/test_merge.py
Original file line number Diff line number Diff line change
Expand Up @@ -2983,3 +2983,23 @@ def test_merge_empty_frames_column_order(left_empty, right_empty):
elif right_empty:
expected.loc[:, ["C", "D"]] = np.nan
tm.assert_frame_equal(result, expected)


@pytest.mark.parametrize("how", ["left", "right", "inner", "outer"])
def test_merge_datetime_and_timedelta(how):
left = DataFrame({"key": Series([1, None], dtype="datetime64[ns]")})
right = DataFrame({"key": Series([1], dtype="timedelta64[ns]")})

msg = (
f"You are trying to merge on {left['key'].dtype} and {right['key'].dtype} "
"columns for key 'key'. If you wish to proceed you should use pd.concat"
)
with pytest.raises(ValueError, match=re.escape(msg)):
left.merge(right, on="key", how=how)

msg = (
f"You are trying to merge on {right['key'].dtype} and {left['key'].dtype} "
"columns for key 'key'. If you wish to proceed you should use pd.concat"
)
with pytest.raises(ValueError, match=re.escape(msg)):
right.merge(left, on="key", how=how)