Skip to content

Commit 4f5ea69

Browse files
authored
BUG: Added raising when merging datetime columns with timedelta columns (#56613)
1 parent b7f3db0 commit 4f5ea69

File tree

3 files changed

+26
-0
lines changed

3 files changed

+26
-0
lines changed

doc/source/whatsnew/v2.2.0.rst

+1
Original file line numberDiff line numberDiff line change
@@ -824,6 +824,7 @@ Reshaping
824824
- Bug in :func:`merge_asof` raising ``TypeError`` when ``by`` dtype is not ``object``, ``int64``, or ``uint64`` (:issue:`22794`)
825825
- Bug in :func:`merge_asof` raising incorrect error for string dtype (:issue:`56444`)
826826
- Bug in :func:`merge_asof` when using a :class:`Timedelta` tolerance on a :class:`ArrowDtype` column (:issue:`56486`)
827+
- Bug in :func:`merge` not raising when merging datetime columns with timedelta columns (:issue:`56455`)
827828
- Bug in :func:`merge` not raising when merging string columns with numeric columns (:issue:`56441`)
828829
- Bug in :func:`merge` returning columns in incorrect order when left and/or right is empty (:issue:`51929`)
829830
- Bug in :meth:`DataFrame.melt` where an exception was raised if ``var_name`` was not a string (:issue:`55948`)

pandas/core/reshape/merge.py

+5
Original file line numberDiff line numberDiff line change
@@ -1526,6 +1526,11 @@ def _maybe_coerce_merge_keys(self) -> None:
15261526
) or (lk.dtype.kind == "M" and rk.dtype.kind == "M"):
15271527
# allows datetime with different resolutions
15281528
continue
1529+
# datetime and timedelta not allowed
1530+
elif lk.dtype.kind == "M" and rk.dtype.kind == "m":
1531+
raise ValueError(msg)
1532+
elif lk.dtype.kind == "m" and rk.dtype.kind == "M":
1533+
raise ValueError(msg)
15291534

15301535
elif is_object_dtype(lk.dtype) and is_object_dtype(rk.dtype):
15311536
continue

pandas/tests/reshape/merge/test_merge.py

+20
Original file line numberDiff line numberDiff line change
@@ -2983,3 +2983,23 @@ def test_merge_empty_frames_column_order(left_empty, right_empty):
29832983
elif right_empty:
29842984
expected.loc[:, ["C", "D"]] = np.nan
29852985
tm.assert_frame_equal(result, expected)
2986+
2987+
2988+
@pytest.mark.parametrize("how", ["left", "right", "inner", "outer"])
2989+
def test_merge_datetime_and_timedelta(how):
2990+
left = DataFrame({"key": Series([1, None], dtype="datetime64[ns]")})
2991+
right = DataFrame({"key": Series([1], dtype="timedelta64[ns]")})
2992+
2993+
msg = (
2994+
f"You are trying to merge on {left['key'].dtype} and {right['key'].dtype} "
2995+
"columns for key 'key'. If you wish to proceed you should use pd.concat"
2996+
)
2997+
with pytest.raises(ValueError, match=re.escape(msg)):
2998+
left.merge(right, on="key", how=how)
2999+
3000+
msg = (
3001+
f"You are trying to merge on {right['key'].dtype} and {left['key'].dtype} "
3002+
"columns for key 'key'. If you wish to proceed you should use pd.concat"
3003+
)
3004+
with pytest.raises(ValueError, match=re.escape(msg)):
3005+
right.merge(left, on="key", how=how)

0 commit comments

Comments
 (0)