@@ -1446,10 +1446,26 @@ def _get_merge_keys(self):
1446
1446
# validate index types are the same
1447
1447
for i , (lk , rk ) in enumerate (zip (left_join_keys , right_join_keys )):
1448
1448
if not is_dtype_equal (lk .dtype , rk .dtype ):
1449
- raise MergeError ("incompatible merge keys [{i}] {lkdtype} and "
1450
- "{rkdtype}, must be the same type"
1451
- .format (i = i , lkdtype = lk .dtype ,
1452
- rkdtype = rk .dtype ))
1449
+ if (is_categorical_dtype (lk .dtype ) and
1450
+ is_categorical_dtype (rk .dtype )):
1451
+ # The generic error message is confusing for categoricals.
1452
+ #
1453
+ # In this function, the join keys include both the original
1454
+ # ones of the merge_asof() call, and also the keys passed
1455
+ # to its by= argument. Unordered but equal categories
1456
+ # are not supported for the former, but will fail
1457
+ # later with a ValueError, so we don't *need* to check
1458
+ # for them here.
1459
+ msg = ("incompatible merge keys [{i}] {lkdtype} and "
1460
+ "{rkdtype}, both sides category, but not equal ones"
1461
+ .format (i = i , lkdtype = repr (lk .dtype ),
1462
+ rkdtype = repr (rk .dtype )))
1463
+ else :
1464
+ msg = ("incompatible merge keys [{i}] {lkdtype} and "
1465
+ "{rkdtype}, must be the same type"
1466
+ .format (i = i , lkdtype = repr (lk .dtype ),
1467
+ rkdtype = repr (rk .dtype )))
1468
+ raise MergeError (msg )
1453
1469
1454
1470
# validate tolerance; must be a Timedelta if we have a DTI
1455
1471
if self .tolerance is not None :
@@ -1462,7 +1478,7 @@ def _get_merge_keys(self):
1462
1478
msg = ("incompatible tolerance {tolerance}, must be compat "
1463
1479
"with type {lkdtype}" .format (
1464
1480
tolerance = type (self .tolerance ),
1465
- lkdtype = lt .dtype ))
1481
+ lkdtype = repr ( lt .dtype ) ))
1466
1482
1467
1483
if is_datetime64_dtype (lt ) or is_datetime64tz_dtype (lt ):
1468
1484
if not isinstance (self .tolerance , Timedelta ):
0 commit comments