Skip to content

Backport PR #43553 on branch 1.3.x (REGR: Outer merge failing with integer and NaN keys) #43566

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
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
2 changes: 1 addition & 1 deletion doc/source/whatsnew/v1.3.4.rst
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ including other versions of pandas.

Fixed regressions
~~~~~~~~~~~~~~~~~
-
- Fixed regression in :meth:`merge` with integer and ``NaN`` keys failing with ``outer`` merge (:issue:`43550`)
-

.. ---------------------------------------------------------------------------
Expand Down
3 changes: 2 additions & 1 deletion pandas/core/reshape/merge.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
Substitution,
)

from pandas.core.dtypes.cast import find_common_type
from pandas.core.dtypes.common import (
ensure_float64,
ensure_int64,
Expand Down Expand Up @@ -911,7 +912,7 @@ def _maybe_add_join_keys(
result_dtype = lvals.dtype
else:
key_col = Index(lvals).where(~mask_left, rvals)
result_dtype = lvals.dtype
result_dtype = find_common_type([lvals.dtype, rvals.dtype])

if result._is_label_reference(name):
result[name] = Series(
Expand Down
29 changes: 29 additions & 0 deletions pandas/tests/reshape/merge/test_merge.py
Original file line number Diff line number Diff line change
Expand Up @@ -2542,3 +2542,32 @@ def test_mergeerror_on_left_index_mismatched_dtypes():
df_2 = DataFrame(data=["X"], columns=["C"], index=[999])
with pytest.raises(MergeError, match="Can only pass argument"):
merge(df_1, df_2, on=["C"], left_index=True)


@pytest.mark.parametrize("dtype", [None, "Int64"])
def test_merge_outer_with_NaN(dtype):
# GH#43550
left = DataFrame({"key": [1, 2], "col1": [1, 2]}, dtype=dtype)
right = DataFrame({"key": [np.nan, np.nan], "col2": [3, 4]}, dtype=dtype)
result = merge(left, right, on="key", how="outer")
expected = DataFrame(
{
"key": [1, 2, np.nan, np.nan],
"col1": [1, 2, np.nan, np.nan],
"col2": [np.nan, np.nan, 3, 4],
},
dtype=dtype,
)
tm.assert_frame_equal(result, expected)

# switch left and right
result = merge(right, left, on="key", how="outer")
expected = DataFrame(
{
"key": [np.nan, np.nan, 1, 2],
"col2": [3, 4, np.nan, np.nan],
"col1": [np.nan, np.nan, 1, 2],
},
dtype=dtype,
)
tm.assert_frame_equal(result, expected)