|
16 | 16 | from pandas.core.dtypes.common import (
|
17 | 17 | is_categorical_dtype,
|
18 | 18 | is_object_dtype,
|
19 |
| - is_float_dtype, |
20 | 19 | )
|
21 | 20 | from pandas import DataFrame, Index, MultiIndex, Series, Categorical
|
22 | 21 | import pandas.util.testing as tm
|
@@ -1412,24 +1411,41 @@ def test_join_multi_dtypes(self, d1, d2):
|
1412 | 1411 | expected.sort_values(['k1', 'k2'], kind='mergesort', inplace=True)
|
1413 | 1412 | tm.assert_frame_equal(result, expected)
|
1414 | 1413 |
|
1415 |
| - @pytest.mark.parametrize('int_vals, float_vals', [ |
1416 |
| - ([1, 2, 3], [1.0, 2.0, 3.0]), |
1417 |
| - ([1, 2, 3], [1.0, 3.0]), |
1418 |
| - ([1, 2], [1.0, 2.0, 3.0]), |
1419 |
| - ([1, 2, 3], [1.1, 2.5, 3.0]), |
| 1414 | + @pytest.mark.parametrize('int_vals, float_vals, exp_vals', [ |
| 1415 | + ([1, 2, 3], [1.0, 2.0, 3.0], {'X': [1, 2, 3], 'Y': [1.0, 2.0, 3.0]}), |
| 1416 | + ([1, 2, 3], [1.0, 3.0], {'X': [1, 3], 'Y': [1.0, 3.0]}), |
| 1417 | + ([1, 2], [1.0, 2.0, 3.0], {'X': [1, 2], 'Y': [1.0, 2.0]}), |
1420 | 1418 | ])
|
1421 |
| - def test_merge_on_ints_floats(self, int_vals, float_vals): |
| 1419 | + def test_merge_on_ints_floats(self, int_vals, float_vals, exp_vals): |
1422 | 1420 | # GH 16572
|
1423 | 1421 | # Check that float column is not cast to object if
|
1424 |
| - # merging on float and int |
| 1422 | + # merging on float and int columns |
1425 | 1423 | A = DataFrame({'X': int_vals})
|
1426 | 1424 | B = DataFrame({'Y': float_vals})
|
| 1425 | + exp_res = DataFrame(exp_vals) |
1427 | 1426 |
|
1428 | 1427 | res = A.merge(B, left_on='X', right_on='Y')
|
1429 |
| - assert is_float_dtype(res['Y'].dtype) |
| 1428 | + assert_frame_equal(res, exp_res) |
1430 | 1429 |
|
1431 | 1430 | res = B.merge(A, left_on='Y', right_on='X')
|
1432 |
| - assert is_float_dtype(res['Y'].dtype) |
| 1431 | + assert_frame_equal(res, exp_res[['Y', 'X']]) |
| 1432 | + |
| 1433 | + def test_merge_on_ints_floats_warning(self): |
| 1434 | + # GH 16572 |
| 1435 | + # merge will produce a warning when merging on int and |
| 1436 | + # float columns where the float values are not exactly |
| 1437 | + # equal to their int representation |
| 1438 | + A = DataFrame({'X': [1, 2, 3]}) |
| 1439 | + B = DataFrame({'Y': [1.1, 2.5, 3.0]}) |
| 1440 | + exp_res = DataFrame({'X': [3], 'Y': [3.0]}) |
| 1441 | + |
| 1442 | + with tm.assert_produces_warning(UserWarning): |
| 1443 | + res = A.merge(B, left_on='X', right_on='Y') |
| 1444 | + assert_frame_equal(res, exp_res) |
| 1445 | + |
| 1446 | + with tm.assert_produces_warning(UserWarning): |
| 1447 | + res = B.merge(A, left_on='Y', right_on='X') |
| 1448 | + assert_frame_equal(res, exp_res[['Y', 'X']]) |
1433 | 1449 |
|
1434 | 1450 |
|
1435 | 1451 | @pytest.fixture
|
|
0 commit comments