swang16
swang16

Reputation: 879

Python Pandas pd.merge_asof: TypeError: 'NoneType' object is not callable

I just discovered pandas merge_asof function and trying to get it to work.
Below is some sample code that reproduces the error I am seeing:

d1 = pd.DataFrame.from_dict({'id':[646327.0, 645714.0, 645462.0],
                             'units1':[1.7723982570833334, 1.7723982570833334, 5.772398257083333]})

d2 = pd.DataFrame.from_dict({'id':[645462.0, 645714.0, 646327.0, 645462.0, 645714.0,
645462.0,
645714.0,
645462.0,
645714.0,
645462.0], 'units2':[0.7723988920833333, 0.7723988920833333, 0.7723988920833333, 3.7723988920833333,
3.7723988920833333,
4.772398892083333,
4.772398892083333,
5.772398892083333,
5.772398892083333,
6.772398892083333]})

And when I try to use pd.merge_asof:

d3 = pd.merge_asof(d1, d2, by = 'id', left_on = 'units1', right_on = 'units2',
                direction='nearest')

I get an error a Nonetype error.
Anyone know what's going on here? Is my syntax incorrect?

Upvotes: 1

Views: 4278

Answers (1)

Michael Hoffman
Michael Hoffman

Reputation: 836

I was able to solve this problem by casting id to an int.

d1['id'] = d1['id'].astype('int')
d2['id'] = d2['id'].astype('int')
d3 = pd.merge_asof(d1, d2, by = 'id', on = 'units', direction='nearest')

Upvotes: 4

Related Questions