|
13 | 13 | from pandas.core.reshape.merge import merge, MergeError
|
14 | 14 | from pandas.util.testing import assert_frame_equal, assert_series_equal
|
15 | 15 | from pandas.core.dtypes.dtypes import CategoricalDtype
|
16 |
| -from pandas.core.dtypes.common import is_categorical_dtype, is_object_dtype |
| 16 | +from pandas.core.dtypes.common import ( |
| 17 | + is_categorical_dtype, |
| 18 | + is_object_dtype, |
| 19 | +) |
17 | 20 | from pandas import DataFrame, Index, MultiIndex, Series, Categorical
|
18 | 21 | import pandas.util.testing as tm
|
19 | 22 | from pandas.api.types import CategoricalDtype as CDT
|
@@ -1408,6 +1411,42 @@ def test_join_multi_dtypes(self, d1, d2):
|
1408 | 1411 | expected.sort_values(['k1', 'k2'], kind='mergesort', inplace=True)
|
1409 | 1412 | tm.assert_frame_equal(result, expected)
|
1410 | 1413 |
|
| 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]}), |
| 1418 | + ]) |
| 1419 | + def test_merge_on_ints_floats(self, int_vals, float_vals, exp_vals): |
| 1420 | + # GH 16572 |
| 1421 | + # Check that float column is not cast to object if |
| 1422 | + # merging on float and int columns |
| 1423 | + A = DataFrame({'X': int_vals}) |
| 1424 | + B = DataFrame({'Y': float_vals}) |
| 1425 | + expected = DataFrame(exp_vals) |
| 1426 | + |
| 1427 | + result = A.merge(B, left_on='X', right_on='Y') |
| 1428 | + assert_frame_equal(result, expected) |
| 1429 | + |
| 1430 | + result = B.merge(A, left_on='Y', right_on='X') |
| 1431 | + assert_frame_equal(result, expected[['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 | + expected = DataFrame({'X': [3], 'Y': [3.0]}) |
| 1441 | + |
| 1442 | + with tm.assert_produces_warning(UserWarning): |
| 1443 | + result = A.merge(B, left_on='X', right_on='Y') |
| 1444 | + assert_frame_equal(result, expected) |
| 1445 | + |
| 1446 | + with tm.assert_produces_warning(UserWarning): |
| 1447 | + result = B.merge(A, left_on='Y', right_on='X') |
| 1448 | + assert_frame_equal(result, expected[['Y', 'X']]) |
| 1449 | + |
1411 | 1450 |
|
1412 | 1451 | @pytest.fixture
|
1413 | 1452 | def left():
|
|
0 commit comments