Skip to content

BUG: merge not raising for String and numeric merges #56441

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 4 commits into from
Dec 18, 2023
Merged
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 @@ -671,6 +671,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 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`)
- Bug in :meth:`DataFrame.melt` where it would not preserve the datetime (:issue:`55254`)
Expand Down
18 changes: 11 additions & 7 deletions pandas/core/reshape/merge.py
Original file line number Diff line number Diff line change
Expand Up @@ -1354,8 +1354,12 @@ def _maybe_coerce_merge_keys(self) -> None:

lk_is_cat = isinstance(lk.dtype, CategoricalDtype)
rk_is_cat = isinstance(rk.dtype, CategoricalDtype)
lk_is_object = is_object_dtype(lk.dtype)
rk_is_object = is_object_dtype(rk.dtype)
lk_is_object_or_string = is_object_dtype(lk.dtype) or is_string_dtype(
lk.dtype
)
rk_is_object_or_string = is_object_dtype(rk.dtype) or is_string_dtype(
rk.dtype
)

# if either left or right is a categorical
# then the must match exactly in categories & ordered
Expand Down Expand Up @@ -1452,14 +1456,14 @@ def _maybe_coerce_merge_keys(self) -> None:
# incompatible dtypes GH 9780, GH 15800

# bool values are coerced to object
elif (lk_is_object and is_bool_dtype(rk.dtype)) or (
is_bool_dtype(lk.dtype) and rk_is_object
elif (lk_is_object_or_string and is_bool_dtype(rk.dtype)) or (
is_bool_dtype(lk.dtype) and rk_is_object_or_string
):
pass

# object values are allowed to be merged
elif (lk_is_object and is_numeric_dtype(rk.dtype)) or (
is_numeric_dtype(lk.dtype) and rk_is_object
elif (lk_is_object_or_string and is_numeric_dtype(rk.dtype)) or (
is_numeric_dtype(lk.dtype) and rk_is_object_or_string
):
inferred_left = lib.infer_dtype(lk, skipna=False)
inferred_right = lib.infer_dtype(rk, skipna=False)
Expand Down Expand Up @@ -1498,7 +1502,7 @@ def _maybe_coerce_merge_keys(self) -> None:
# allows datetime with different resolutions
continue

elif lk_is_object and rk_is_object:
elif is_object_dtype(lk.dtype) and is_object_dtype(rk.dtype):
continue

# Houston, we have a problem!
Expand Down
11 changes: 8 additions & 3 deletions pandas/tests/reshape/merge/test_join.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
import numpy as np
import pytest

import pandas.util._test_decorators as td

import pandas as pd
from pandas import (
Categorical,
Expand Down Expand Up @@ -118,7 +120,10 @@ def test_handle_overlap_arbitrary_key(self, df, df2):
assert "key1.foo" in joined
assert "key2.bar" in joined

def test_join_on(self, target_source):
@pytest.mark.parametrize(
"infer_string", [False, pytest.param(True, marks=td.skip_if_no("pyarrow"))]
)
def test_join_on(self, target_source, infer_string):
target, source = target_source

merged = target.join(source, on="C")
Expand Down Expand Up @@ -150,8 +155,8 @@ def test_join_on(self, target_source):
# overlap
source_copy = source.copy()
msg = (
"You are trying to merge on float64 and object columns for key 'A'. "
"If you wish to proceed you should use pd.concat"
"You are trying to merge on float64 and object|string columns for key "
"'A'. If you wish to proceed you should use pd.concat"
)
with pytest.raises(ValueError, match=msg):
target.join(source_copy, on="A")
Expand Down