Skip to content

Commit

Permalink
Backport PR pandas-dev#56613 on branch 2.2.x (BUG: Added raising when…
Browse files Browse the repository at this point in the history
… merging datetime columns with timedelta columns) (pandas-dev#56658)

Backport PR pandas-dev#56613: BUG: Added raising when merging datetime columns with timedelta columns

Co-authored-by: Huanghz2001 <[email protected]>
  • Loading branch information
meeseeksmachine and Huanghz2001 authored Dec 28, 2023
1 parent f99f4d6 commit 80ba450
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 0 deletions.
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 @@ -824,6 +824,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 @@ -2988,3 +2988,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)

0 comments on commit 80ba450

Please sign in to comment.