Skip to content

Commit ba5aa95

Browse files
committed
use elifs and parametrize tests
1 parent dd9d494 commit ba5aa95

File tree

2 files changed

+68
-53
lines changed

2 files changed

+68
-53
lines changed

pandas/core/reshape/merge.py

+31-20
Original file line numberDiff line numberDiff line change
@@ -963,30 +963,41 @@ def _maybe_coerce_merge_keys(self):
963963
elif lib.infer_dtype(lk) == lib.infer_dtype(rk):
964964
pass
965965

966-
else:
967-
968-
# Check if we are trying to merge on obviously
969-
# incompatible dtypes GH 9780
966+
# Check if we are trying to merge on obviously
967+
# incompatible dtypes GH 9780
968+
elif is_numeric_dtype(lk) and not is_numeric_dtype(rk):
969+
msg = ("You are trying to merge on {lk_dtype} and "
970+
"{rk_dtype} columns. If you wish to proceed "
971+
"you should use pd.concat".format(lk_dtype=lk.dtype,
972+
rk_dtype=rk.dtype))
973+
raise ValueError(msg)
974+
elif not is_numeric_dtype(lk) and is_numeric_dtype(rk):
970975
msg = ("You are trying to merge on {lk_dtype} and "
971976
"{rk_dtype} columns. If you wish to proceed "
972977
"you should use pd.concat".format(lk_dtype=lk.dtype,
973978
rk_dtype=rk.dtype))
974-
if is_numeric_dtype(lk) and not is_numeric_dtype(rk):
975-
raise ValueError(msg)
976-
elif not is_numeric_dtype(lk) and is_numeric_dtype(rk):
977-
raise ValueError(msg)
978-
elif is_datetimelike(lk) and not is_datetimelike(rk):
979-
raise ValueError(msg)
980-
elif not is_datetimelike(lk) and is_datetimelike(rk):
981-
raise ValueError(msg)
982-
983-
# Houston, we have a problem!
984-
# let's coerce to object if the dtypes aren't
985-
# categorical, otherwise coerce to the category
986-
# dtype. If we coerced categories to object,
987-
# then we would lose type information on some
988-
# columns, and end up trying to merge
989-
# incompatible dtypes. See GH 16900.
979+
raise ValueError(msg)
980+
elif is_datetimelike(lk) and not is_datetimelike(rk):
981+
msg = ("You are trying to merge on {lk_dtype} and "
982+
"{rk_dtype} columns. If you wish to proceed "
983+
"you should use pd.concat".format(lk_dtype=lk.dtype,
984+
rk_dtype=rk.dtype))
985+
raise ValueError(msg)
986+
elif not is_datetimelike(lk) and is_datetimelike(rk):
987+
msg = ("You are trying to merge on {lk_dtype} and "
988+
"{rk_dtype} columns. If you wish to proceed "
989+
"you should use pd.concat".format(lk_dtype=lk.dtype,
990+
rk_dtype=rk.dtype))
991+
raise ValueError(msg)
992+
993+
# Houston, we have a problem!
994+
# let's coerce to object if the dtypes aren't
995+
# categorical, otherwise coerce to the category
996+
# dtype. If we coerced categories to object,
997+
# then we would lose type information on some
998+
# columns, and end up trying to merge
999+
# incompatible dtypes. See GH 16900.
1000+
else:
9901001
if name in self.left.columns:
9911002
typ = lk.categories.dtype if lk_is_cat else object
9921003
self.left = self.left.assign(

pandas/tests/reshape/merge/test_merge.py

+37-33
Original file line numberDiff line numberDiff line change
@@ -1371,42 +1371,46 @@ def f():
13711371
pytest.raises(NotImplementedError, f)
13721372

13731373

1374-
@pytest.fixture
1375-
def df():
1376-
return DataFrame(
1377-
{'A': ['foo', 'bar'],
1378-
'B': Series(['foo', 'bar']).astype('category'),
1379-
'C': [1, 2],
1380-
'D': [1.0, 2.0],
1381-
'E': Series([1, 2], dtype='uint64'),
1382-
'F': Series([1, 2], dtype='int32')})
1374+
class TestMergeDtypes(object):
13831375

1376+
@pytest.mark.parametrize('right_vals', [
1377+
['foo', 'bar'],
1378+
Series(['foo', 'bar']).astype('category'),
1379+
[1, 2],
1380+
[1.0, 2.0],
1381+
Series([1, 2], dtype='uint64'),
1382+
Series([1, 2], dtype='int32')
1383+
]
1384+
)
1385+
def test_different(self, right_vals):
1386+
1387+
left = DataFrame({'A': ['foo', 'bar'],
1388+
'B': Series(['foo', 'bar']).astype('category'),
1389+
'C': [1, 2],
1390+
'D': [1.0, 2.0],
1391+
'E': Series([1, 2], dtype='uint64'),
1392+
'F': Series([1, 2], dtype='int32')})
1393+
right = DataFrame({'A': right_vals})
13841394

1385-
class TestMergeDtypes(object):
1395+
# GH 9780
1396+
# We allow merging on object and categorical cols and cast
1397+
# categorical cols to object
1398+
if (is_categorical_dtype(right['A'].dtype) or
1399+
is_object_dtype(right['A'].dtype)):
1400+
result = pd.merge(left, right, on='A')
1401+
assert is_object_dtype(result.A.dtype)
13861402

1387-
def test_different(self, df):
1388-
1389-
left = df
1390-
for col in df.columns:
1391-
right = DataFrame({'A': df[col]})
1392-
# GH 9780
1393-
# We allow merging on object and categorical cols and cast
1394-
# categorical cols to object
1395-
if (is_categorical_dtype(right['A'].dtype) or
1396-
is_object_dtype(right['A'].dtype)):
1397-
result = pd.merge(left, right, on='A')
1398-
assert is_object_dtype(result.A.dtype)
1399-
# GH 9780
1400-
# We raise for merging on object col and int/float col and
1401-
# merging on categorical col and int/float col
1402-
else:
1403-
msg = ("You are trying to merge on "
1404-
"{lk_dtype} and {rk_dtype} columns. "
1405-
"If you wish to proceed you should use "
1406-
"pd.concat".format(lk_dtype=left['A'].dtype,
1407-
rk_dtype=right['A'].dtype))
1408-
with tm.assert_raises_regex(ValueError, msg):
1409-
pd.merge(left, right, on='A')
1403+
# GH 9780
1404+
# We raise for merging on object col and int/float col and
1405+
# merging on categorical col and int/float col
1406+
else:
1407+
msg = ("You are trying to merge on "
1408+
"{lk_dtype} and {rk_dtype} columns. "
1409+
"If you wish to proceed you should use "
1410+
"pd.concat".format(lk_dtype=left['A'].dtype,
1411+
rk_dtype=right['A'].dtype))
1412+
with tm.assert_raises_regex(ValueError, msg):
1413+
pd.merge(left, right, on='A')
14101414

14111415
@pytest.mark.parametrize('d1', [np.int64, np.int32,
14121416
np.int16, np.int8, np.uint8])

0 commit comments

Comments
 (0)