Skip to content

Commit 6250341

Browse files
Backport PR #43553: REGR: Outer merge failing with integer and NaN keys (#43566)
Co-authored-by: Shoham Debnath <[email protected]>
1 parent f4d1e90 commit 6250341

File tree

3 files changed

+32
-2
lines changed

3 files changed

+32
-2
lines changed

doc/source/whatsnew/v1.3.4.rst

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ including other versions of pandas.
1414

1515
Fixed regressions
1616
~~~~~~~~~~~~~~~~~
17-
-
17+
- Fixed regression in :meth:`merge` with integer and ``NaN`` keys failing with ``outer`` merge (:issue:`43550`)
1818
-
1919

2020
.. ---------------------------------------------------------------------------

pandas/core/reshape/merge.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
Substitution,
3737
)
3838

39+
from pandas.core.dtypes.cast import find_common_type
3940
from pandas.core.dtypes.common import (
4041
ensure_float64,
4142
ensure_int64,
@@ -911,7 +912,7 @@ def _maybe_add_join_keys(
911912
result_dtype = lvals.dtype
912913
else:
913914
key_col = Index(lvals).where(~mask_left, rvals)
914-
result_dtype = lvals.dtype
915+
result_dtype = find_common_type([lvals.dtype, rvals.dtype])
915916

916917
if result._is_label_reference(name):
917918
result[name] = Series(

pandas/tests/reshape/merge/test_merge.py

+29
Original file line numberDiff line numberDiff line change
@@ -2542,3 +2542,32 @@ def test_mergeerror_on_left_index_mismatched_dtypes():
25422542
df_2 = DataFrame(data=["X"], columns=["C"], index=[999])
25432543
with pytest.raises(MergeError, match="Can only pass argument"):
25442544
merge(df_1, df_2, on=["C"], left_index=True)
2545+
2546+
2547+
@pytest.mark.parametrize("dtype", [None, "Int64"])
2548+
def test_merge_outer_with_NaN(dtype):
2549+
# GH#43550
2550+
left = DataFrame({"key": [1, 2], "col1": [1, 2]}, dtype=dtype)
2551+
right = DataFrame({"key": [np.nan, np.nan], "col2": [3, 4]}, dtype=dtype)
2552+
result = merge(left, right, on="key", how="outer")
2553+
expected = DataFrame(
2554+
{
2555+
"key": [1, 2, np.nan, np.nan],
2556+
"col1": [1, 2, np.nan, np.nan],
2557+
"col2": [np.nan, np.nan, 3, 4],
2558+
},
2559+
dtype=dtype,
2560+
)
2561+
tm.assert_frame_equal(result, expected)
2562+
2563+
# switch left and right
2564+
result = merge(right, left, on="key", how="outer")
2565+
expected = DataFrame(
2566+
{
2567+
"key": [np.nan, np.nan, 1, 2],
2568+
"col2": [3, 4, np.nan, np.nan],
2569+
"col1": [np.nan, np.nan, 1, 2],
2570+
},
2571+
dtype=dtype,
2572+
)
2573+
tm.assert_frame_equal(result, expected)

0 commit comments

Comments
 (0)