Repository: pandas-dev/pandas
Pull request: Fix 18068: Updates merge_asof error, now outputs datatypes
- [ ] closes #18068
- [ ] tests added / passed
- [ ] passes `git diff upstream/master -u -- "*.py" | flake8 --diff`
- [ ] whatsnew entry


Problem statement:
ERR: better error message on merging for incompat types
would be nice to have the types outputted in the error message

```
In [23]:         left = pd.DataFrame({'a': [1, 5, 10],
    ...:                              'left_val': ['a', 'b', 'c']})
    ...:         right = pd.DataFrame({'a': [1, 2, 3, 6, 7],
    ...:                               'right_val': [1, 2, 3, 6, 7]})
    ...: 
    ...: 

In [24]: pd.merge_asof(left, right, on='a')
Out[24]: 
    a left_val  right_val
0   1        a          1
1   5        b          3
2  10        c          7

In [25]: pd.merge_asof(left, right.assign(a=right.a.astype('i4')), on='a')
MergeError: incompatible merge keys, must be the same type
```

Hints from issue discussion:
Hi,

I'm new to contributing to pandas and would love to take a crack at this!

Commit message: Release notes for 0.21.1 updated with fix 18068

Patch to review:
diff --git a/doc/source/whatsnew/v0.21.1.txt b/doc/source/whatsnew/v0.21.1.txt
index 4adafe7c06450..b7b8240a8d77e 100644
--- a/doc/source/whatsnew/v0.21.1.txt
+++ b/doc/source/whatsnew/v0.21.1.txt
@@ -102,7 +102,7 @@ Sparse
 Reshaping
 ^^^^^^^^^
 
--
+- Error message in ``pd.merge_asof()`` for key datatype mismatch now includes datatype of left and right key (:issue:`18068`)
 -
 -
 
diff --git a/pandas/core/reshape/merge.py b/pandas/core/reshape/merge.py
index e409090e76944..e8dfd763baed2 100644
--- a/pandas/core/reshape/merge.py
+++ b/pandas/core/reshape/merge.py
@@ -1253,10 +1253,12 @@ def _get_merge_keys(self):
          join_names) = super(_AsOfMerge, self)._get_merge_keys()
 
         # validate index types are the same
-        for lk, rk in zip(left_join_keys, right_join_keys):
+        for i, (lk, rk) in enumerate(zip(left_join_keys, right_join_keys)):
             if not is_dtype_equal(lk.dtype, rk.dtype):
-                raise MergeError("incompatible merge keys, "
-                                 "must be the same type")
+                raise MergeError("incompatible merge keys [{i} {lkdtype} and "
+                                 "{rkdtype}], must be the same type"
+                                 .format(i=i, lkdtype=lk.dtype,
+                                         rkdtype=rk.dtype))
 
         # validate tolerance; must be a Timedelta if we have a DTI
         if self.tolerance is not None:


Code review comments:
diff_hunk: @@ -1253,10 +1253,12 @@ def _get_merge_keys(self):
          join_names) = super(_AsOfMerge, self)._get_merge_keys()
 
         # validate index types are the same
-        for lk, rk in zip(left_join_keys, right_join_keys):
+        for i, (lk, rk) in enumerate(zip(left_join_keys, right_join_keys)):
             if not is_dtype_equal(lk.dtype, rk.dtype):
-                raise MergeError("incompatible merge keys, "
-                                 "must be the same type")
+                raise MergeError("incompatible merge keys [{i} {lkdtype} and "
path: pandas/core/reshape/merge.py
text: @user1:
[{i}] other wise this is confusing