jcp
jcp

Reputation: 851

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

Upvotes: 11

Views: 8573

Answers (3)

Itamar Mushkin
Itamar Mushkin

Reputation: 2905

To add to @doraelee's answer,
not only does the 'by' column must be of type int and not float,
it also can't be int32 (can be int64).

Now, I understand float can't be used in the by column, of course, but I can't understand why int32 isn't valid; however this is the result I've found.

Example of failing code (pandas version=2.1.1), with a slight change to the MCVE provided in the question:

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'])

a = a.astype(dict(foo=int)) # the whole thing will pass if we replace int with 'int64'
b = b.astype(dict(foo=int)) # here as well, of course

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

Upvotes: 0

Doraelee
Doraelee

Reputation: 161

The "By" variable must not be float.

It can be "integer" or "string".

Upvotes: 4

Ashish Anand
Ashish Anand

Reputation: 2743

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

Upvotes: 0

Related Questions