From 2c2459095026e61a9f2beca40433c60e3e843e69 Mon Sep 17 00:00:00 2001 From: Joris Van den Bossche Date: Fri, 11 May 2018 10:57:14 +0200 Subject: [PATCH] Don't raise warning on merging int and float with nan --- pandas/core/reshape/merge.py | 4 ++-- pandas/tests/reshape/merge/test_merge.py | 7 +++++++ 2 files changed, 9 insertions(+), 2 deletions(-) diff --git a/pandas/core/reshape/merge.py b/pandas/core/reshape/merge.py index 0204e655bfa2c..4d8897fb7c811 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 436fe8f9f5d7e..8e639edd34b18 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"]),