Skip to content
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

BUG: merge should upcast to highest resolution #56310

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -761,6 +761,7 @@ Datetimelike
- Bug in parsing datetime strings with nanosecond resolution with non-ISO8601 formats incorrectly truncating sub-microsecond components (:issue:`56051`)
- Bug in parsing datetime strings with sub-second resolution and trailing zeros incorrectly inferring second or millisecond resolution (:issue:`55737`)
- Bug in the results of :func:`to_datetime` with an floating-dtype argument with ``unit`` not matching the pointwise results of :class:`Timestamp` (:issue:`56037`)
- Fixed bug in :meth:`DataFrame.merge` not being able to join on ``datetime64`` columns of differing resolutions (:issue:`55212`)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

in main they are able to join just gives the wrong result reso?

- Fixed regression where :func:`concat` would raise an error when concatenating ``datetime64`` columns with differing resolutions (:issue:`53641`)

Timedelta
Expand Down
11 changes: 2 additions & 9 deletions pandas/core/reshape/merge.py
Original file line number Diff line number Diff line change
Expand Up @@ -1083,18 +1083,11 @@ def _maybe_add_join_keys(
result_dtype = lvals.dtype
else:
key_col = Index(lvals)
result_dtype = find_common_type([lvals.dtype, rvals.dtype])
key_col = key_col.astype(result_dtype, copy=False)
if left_indexer is not None:
mask_left = left_indexer == -1
key_col = key_col.where(~mask_left, rvals)
result_dtype = find_common_type([lvals.dtype, rvals.dtype])
if (
lvals.dtype.kind == "M"
and rvals.dtype.kind == "M"
and result_dtype.kind == "O"
):
# TODO(non-nano) Workaround for common_type not dealing
# with different resolutions
result_dtype = key_col.dtype

if result._is_label_reference(name):
result[name] = result._constructor_sliced(
Expand Down
25 changes: 19 additions & 6 deletions pandas/tests/reshape/merge/test_merge.py
Original file line number Diff line number Diff line change
Expand Up @@ -2816,7 +2816,8 @@ def test_merge_arrow_and_numpy_dtypes(dtype):

@pytest.mark.parametrize("how", ["inner", "left", "outer", "right"])
@pytest.mark.parametrize("tz", [None, "America/Chicago"])
def test_merge_datetime_different_resolution(tz, how):
@pytest.mark.parametrize("unit", ["us", "ms", "s"])
def test_merge_datetime_different_resolution(tz, how, unit):
# https://github.com/pandas-dev/pandas/issues/53200
vals = [
pd.Timestamp(2023, 5, 12, tz=tz),
Expand All @@ -2826,19 +2827,31 @@ def test_merge_datetime_different_resolution(tz, how):
df1 = DataFrame({"t": vals[:2], "a": [1.0, 2.0]})
df1["t"] = df1["t"].dt.as_unit("ns")
df2 = DataFrame({"t": vals[1:], "b": [1.0, 2.0]})
df2["t"] = df2["t"].dt.as_unit("s")
df2["t"] = df2["t"].dt.as_unit(unit)

expected = DataFrame({"t": vals, "a": [1.0, 2.0, np.nan], "b": [np.nan, 1.0, 2.0]})
expected["t"] = expected["t"].dt.as_unit("ns")
if how == "inner":
expected = expected.iloc[[1]].reset_index(drop=True)
expected1 = expected.iloc[[1]].reset_index(drop=True)
expected2 = expected1
elif how == "left":
expected = expected.iloc[[0, 1]]
expected1 = expected.iloc[[0, 1]]
expected2 = expected.iloc[[1, 2]].reset_index(drop=True)
elif how == "right":
expected = expected.iloc[[1, 2]].reset_index(drop=True)
expected1 = expected.iloc[[1, 2]].reset_index(drop=True)
expected2 = expected.iloc[[0, 1]]
else:
expected1 = expected
expected2 = expected

result = df1.merge(df2, on="t", how=how)
tm.assert_frame_equal(result, expected)
tm.assert_frame_equal(result, expected1)

# Check lower resolution to higher resolution also works
# GH55212
expected2 = expected2[["t", "b", "a"]]
result1 = df2.merge(df1, on="t", how=how)
tm.assert_frame_equal(result1, expected2)


def test_merge_multiindex_single_level():
Expand Down
Loading