Skip to content

Commit 95a3b07

Browse files
REGR: Outer merge failing with integer and NaN keys (pandas-dev#43553)
* REGR: Outer merge failing with integer and NaN keys * REGR: Outer merge failing with integer and NaN keys * changed test as per suggestion
1 parent 7277952 commit 95a3b07

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
@@ -37,6 +37,7 @@
3737
Substitution,
3838
)
3939

40+
from pandas.core.dtypes.cast import find_common_type
4041
from pandas.core.dtypes.common import (
4142
ensure_float64,
4243
ensure_int64,
@@ -912,7 +913,7 @@ def _maybe_add_join_keys(
912913
result_dtype = lvals.dtype
913914
else:
914915
key_col = Index(lvals).where(~mask_left, rvals)
915-
result_dtype = lvals.dtype
916+
result_dtype = find_common_type([lvals.dtype, rvals.dtype])
916917

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

pandas/tests/reshape/merge/test_merge.py

+29
Original file line numberDiff line numberDiff line change
@@ -2571,3 +2571,32 @@ def test_mergeerror_on_left_index_mismatched_dtypes():
25712571
df_2 = DataFrame(data=["X"], columns=["C"], index=[999])
25722572
with pytest.raises(MergeError, match="Can only pass argument"):
25732573
merge(df_1, df_2, on=["C"], left_index=True)
2574+
2575+
2576+
@pytest.mark.parametrize("dtype", [None, "Int64"])
2577+
def test_merge_outer_with_NaN(dtype):
2578+
# GH#43550
2579+
left = DataFrame({"key": [1, 2], "col1": [1, 2]}, dtype=dtype)
2580+
right = DataFrame({"key": [np.nan, np.nan], "col2": [3, 4]}, dtype=dtype)
2581+
result = merge(left, right, on="key", how="outer")
2582+
expected = DataFrame(
2583+
{
2584+
"key": [1, 2, np.nan, np.nan],
2585+
"col1": [1, 2, np.nan, np.nan],
2586+
"col2": [np.nan, np.nan, 3, 4],
2587+
},
2588+
dtype=dtype,
2589+
)
2590+
tm.assert_frame_equal(result, expected)
2591+
2592+
# switch left and right
2593+
result = merge(right, left, on="key", how="outer")
2594+
expected = DataFrame(
2595+
{
2596+
"key": [np.nan, np.nan, 1, 2],
2597+
"col2": [3, 4, np.nan, np.nan],
2598+
"col1": [np.nan, np.nan, 1, 2],
2599+
},
2600+
dtype=dtype,
2601+
)
2602+
tm.assert_frame_equal(result, expected)

0 commit comments

Comments
 (0)