BUG: Merge_asof error message unclear when attempting to join on missing values · Issue #23189 · pandas-dev/pandas (original) (raw)
The merge_asof function gives confusing error message when trying to join on columns which contain null values based on it failing the is_monotonic check
See example:
left = pd.DataFrame({'a': [10.0, np.nan, 3.0, 12.0, 15.0], 'left_val': ['a', 'b', 'c', 'd', 'e']}) right = pd.DataFrame({'a': [1.0, 5.0, 10.0, 12.0], 'right_val': [1, 6, 11, 15]})
As a responsible pandas user I then sort my left DataFrame
left_sorted = left.sort_values(by="a")
and try using merge_asof
merge_asof(left_sorted, right, on='a')
ValueError: left keys must be sorted
This can lead to scratching of heads, because hey, the DataFrame was just sorted, and it kind of looks sorted, (also doesn't appear in docs that this isn't allowed).
The proposed change will raise a more meaningful error to lead the user to the cause of the problem.
ValueError: Merge keys contain null values on left side
Working on a pull request with @tolaa06 which should resolve this issue