Skip to content

Commit 0e00151

Browse files
jorisvandenbosschejreback
authored andcommitted
Don't raise warning on merging int and float with nan (#21011)
1 parent 17a0ca1 commit 0e00151

File tree

2 files changed

+9
-2
lines changed

2 files changed

+9
-2
lines changed

pandas/core/reshape/merge.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -955,14 +955,14 @@ def _maybe_coerce_merge_keys(self):
955955

956956
# check whether ints and floats
957957
elif is_integer_dtype(rk) and is_float_dtype(lk):
958-
if not (lk == lk.astype(rk.dtype)).all():
958+
if not (lk == lk.astype(rk.dtype))[~np.isnan(lk)].all():
959959
warnings.warn('You are merging on int and float '
960960
'columns where the float values '
961961
'are not equal to their int '
962962
'representation', UserWarning)
963963

964964
elif is_float_dtype(rk) and is_integer_dtype(lk):
965-
if not (rk == rk.astype(lk.dtype)).all():
965+
if not (rk == rk.astype(lk.dtype))[~np.isnan(rk)].all():
966966
warnings.warn('You are merging on int and float '
967967
'columns where the float values '
968968
'are not equal to their int '

pandas/tests/reshape/merge/test_merge.py

+7
Original file line numberDiff line numberDiff line change
@@ -1519,6 +1519,13 @@ def test_merge_on_ints_floats_warning(self):
15191519
result = B.merge(A, left_on='Y', right_on='X')
15201520
assert_frame_equal(result, expected[['Y', 'X']])
15211521

1522+
# test no warning if float has NaNs
1523+
B = DataFrame({'Y': [np.nan, np.nan, 3.0]})
1524+
1525+
with tm.assert_produces_warning(None):
1526+
result = B.merge(A, left_on='Y', right_on='X')
1527+
assert_frame_equal(result, expected[['Y', 'X']])
1528+
15221529
@pytest.mark.parametrize('df1_vals, df2_vals', [
15231530
([0, 1, 2], ["0", "1", "2"]),
15241531
([0.0, 1.0, 2.0], ["0", "1", "2"]),

0 commit comments

Comments
 (0)