Skip to content

REGR: Outer merge failing with integer and NaN keys #43553

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 3 commits into from
Sep 14, 2021
Merged
Show file tree
Hide file tree
Changes from 2 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 @@ -37,6 +37,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 @@ -912,7 +913,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
15 changes: 15 additions & 0 deletions pandas/tests/reshape/merge/test_merge.py
Original file line number Diff line number Diff line change
Expand Up @@ -2571,3 +2571,18 @@ 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)


def test_merge_outer_with_NaN():
Copy link
Contributor

Choose a reason for hiding this comment

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

can you parameterize with Int64 as well

Copy link
Member

Choose a reason for hiding this comment

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

Can we switch left and right too? Changing lvals to rvals above would have fixed the issue but caused the inverse to fail

Copy link
Member Author

Choose a reason for hiding this comment

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

done and done

# GH#43550
left = DataFrame({"key": [1, 2], "col1": [1, 2]})
right = DataFrame({"key": [np.nan, np.nan], "col2": [3, 4]})
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],
}
)
tm.assert_frame_equal(result, expected)