diff --git a/pandas/core/reshape/merge.py b/pandas/core/reshape/merge.py index 0204e655bfa2c3..4d8897fb7c811d 100644 --- a/pandas/core/reshape/merge.py +++ b/pandas/core/reshape/merge.py @@ -955,14 +955,14 @@ def _maybe_coerce_merge_keys(self): # check whether ints and floats elif is_integer_dtype(rk) and is_float_dtype(lk): - if not (lk == lk.astype(rk.dtype)).all(): + if not (lk == lk.astype(rk.dtype))[~np.isnan(lk)].all(): warnings.warn('You are merging on int and float ' 'columns where the float values ' 'are not equal to their int ' 'representation', UserWarning) elif is_float_dtype(rk) and is_integer_dtype(lk): - if not (rk == rk.astype(lk.dtype)).all(): + if not (rk == rk.astype(lk.dtype))[~np.isnan(rk)].all(): warnings.warn('You are merging on int and float ' 'columns where the float values ' 'are not equal to their int ' diff --git a/pandas/tests/reshape/merge/test_merge.py b/pandas/tests/reshape/merge/test_merge.py index 436fe8f9f5d7e1..8e639edd34b184 100644 --- a/pandas/tests/reshape/merge/test_merge.py +++ b/pandas/tests/reshape/merge/test_merge.py @@ -1519,6 +1519,13 @@ def test_merge_on_ints_floats_warning(self): result = B.merge(A, left_on='Y', right_on='X') assert_frame_equal(result, expected[['Y', 'X']]) + # test no warning if float has NaNs + B = DataFrame({'Y': [np.nan, np.nan, 3.0]}) + + with tm.assert_produces_warning(None): + result = B.merge(A, left_on='Y', right_on='X') + assert_frame_equal(result, expected[['Y', 'X']]) + @pytest.mark.parametrize('df1_vals, df2_vals', [ ([0, 1, 2], ["0", "1", "2"]), ([0.0, 1.0, 2.0], ["0", "1", "2"]),