Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

pandas merge_asof: ambiguous argument types error

I'm trying to use merge_asof from Pandas and I've been getting the error:

TypeError: Function call with ambiguous argument types

Reproducible example:

import pandas as pd

a = pd.DataFrame({'foo': [1., 2.], 'bar': ['2019-01-01 00:00:10', '2019-01-01 00:00:20']})
b = pd.DataFrame({'foo': [2., 5.], 'baz': ['2019-01-01 00:00:05', '2019-01-01 00:00:25']})
a['bar'] = pd.to_datetime(a['bar'])
b['baz'] = pd.to_datetime(b['baz'])

pd.merge_asof(a,
              b,
              left_on='bar',
              right_on='baz',
              direction='backward',
              by='foo',
              allow_exact_matches=False)

I've tried to inspect the pandas.core.reshape.merge file but had no luck solving the problem

like image 642
jcp Avatar asked Oct 15 '19 21:10

jcp


People also ask

What is Asof merge in PostgreSQL?

merge_asof () function Perform an asof merge. This is similar to a left-join except that we match on nearest key rather than equal keys. Both DataFrames must be sorted by the key.

How to use merge_Asof () function in Python?

merge_asof () function 1 Perform an asof merge. ... 2 The default is “backward” and is compatible in versions below 0.20.0. ... 3 Optionally match on equivalent keys with 'by' before searching with 'on'. ... 4 Returns: merged: DataFrame. ... 5 Download the above Notebook from here. 6 Previous: merge_ordered () function Next: concat () function

How do I merge two DataFrames in Python?

Perform an asof merge. This is similar to a left-join except that we match on nearest key rather than equal keys. Both DataFrames must be sorted by the key. For each row in the left DataFrame: A “backward” search selects the last row in the right DataFrame whose ‘on’ key is less than or equal to the left’s key.


1 Answers

Problem of pd.merge_asof( left, right, on=None,by=None) is due to "ON" parameter. It must be of date type otherwise it will throw error of ambiguous argument type.so we need to convert them to pandas datetime objects using pd.to_datetime() to solve the error.

enter image description here

like image 71
Ashish Anand Avatar answered Oct 10 '22 06:10

Ashish Anand