merge_asof doesn't allow float tolerance for float merges · Issue #22981 · pandas-dev/pandas (original) (raw)

The merge_asof function allows you to merge on float values but doesn't allow tolerance value when performing this merge, unlike for integer and datetime merges. I'm currently working on a pull request to fix this by removing the error being thrown for non-integer numerical tolerances.

left = pd.DataFrame({'a': [1.1, 3.5, 10.9], 'left_val': ['a', 'b', 'c']})

right = pd.DataFrame({'a': [1.0, 2.5, 3.3, 7.5, 11.5], 'right_val': [1.0, 2.5, 3.3, 7.5, 11.5]})

expected = pd.DataFrame({'a': [1.1, 3.5, 10.9], 'left_val': ['a', 'b', 'c'], 'right_val': [1, 3.3, np.nan]})

result = pd.merge_asof(left, right, on='a', direction='nearest', tolerance=0.5)

MergeError: key must be integer or timestamp

Expected output:

     a left_val  right_val
0   1.1        a        1.0
1   3.5        b        3.3
2  10.9        c        NaN