Skip to content

Commit 80ba450

Browse files
Backport PR #56613 on branch 2.2.x (BUG: Added raising when merging datetime columns with timedelta columns) (#56658)
Backport PR #56613: BUG: Added raising when merging datetime columns with timedelta columns Co-authored-by: Huanghz2001 <[email protected]>
1 parent f99f4d6 commit 80ba450

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
@@ -2988,3 +2988,23 @@ def test_merge_empty_frames_column_order(left_empty, right_empty):
29882988
elif right_empty:
29892989
expected.loc[:, ["C", "D"]] = np.nan
29902990
tm.assert_frame_equal(result, expected)
2991+
2992+
2993+
@pytest.mark.parametrize("how", ["left", "right", "inner", "outer"])
2994+
def test_merge_datetime_and_timedelta(how):
2995+
left = DataFrame({"key": Series([1, None], dtype="datetime64[ns]")})
2996+
right = DataFrame({"key": Series([1], dtype="timedelta64[ns]")})
2997+
2998+
msg = (
2999+
f"You are trying to merge on {left['key'].dtype} and {right['key'].dtype} "
3000+
"columns for key 'key'. If you wish to proceed you should use pd.concat"
3001+
)
3002+
with pytest.raises(ValueError, match=re.escape(msg)):
3003+
left.merge(right, on="key", how=how)
3004+
3005+
msg = (
3006+
f"You are trying to merge on {right['key'].dtype} and {left['key'].dtype} "
3007+
"columns for key 'key'. If you wish to proceed you should use pd.concat"
3008+
)
3009+
with pytest.raises(ValueError, match=re.escape(msg)):
3010+
right.merge(left, on="key", how=how)

0 commit comments

Comments
 (0)