Julian
Julian

Reputation: 603

Python pandas: merge_asof throws TypeError: 'NoneType' object is not callable

The pandas merge_asof function documentation page gives an example which works fine for me:

left = pd.DataFrame({'a': [1, 5, 10], 'left_val': ['a', 'b', 'c']})
right = pd.DataFrame({'a': [1, 2, 3, 6, 7], 'right_val': [1, 2, 3, 6, 7]})
pd.merge_asof(left, right, on='a')

enter image description here

However, when trying this with my own dataframes, I get a TypeError

df1 = pd.DataFrame(
    {
    "col1": [1,2,3],
    "col2": ["a", "b", "c"]
    }
)
df1 = df1.sort_values(by="col2")

df2 = pd.DataFrame(
    {
    "col3": [20,30,40,50],
    "col2": ["b", "c", "d", "b"]
    }
)
df2 = df2.sort_values(by="col2")
df1
df2
pd.merge_asof(df1, df2, on='col2', direction='nearest')

enter image description here

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
 in ()
----> 1 pd.merge_asof(df1, df2, on='col2', direction='nearest')

C:\ProgramData\Anaconda3\lib\site-packages\pandas\core\reshape\merge.py in merge_asof(left, right, on, left_on, right_on, left_index, right_index, by, left_by, right_by, suffixes, tolerance, allow_exact_matches, direction)
    477                     allow_exact_matches=allow_exact_matches,
    478                     direction=direction)
--> 479     return op.get_result()
    480 
    481 

C:\ProgramData\Anaconda3\lib\site-packages\pandas\core\reshape\merge.py in get_result(self)
   1167 
   1168     def get_result(self):
-> 1169         join_index, left_indexer, right_indexer = self._get_join_info()
   1170 
   1171         # this is a bit kludgy

C:\ProgramData\Anaconda3\lib\site-packages\pandas\core\reshape\merge.py in _get_join_info(self)
    775         else:
    776             (left_indexer,
--> 777              right_indexer) = self._get_join_indexers()
    778 
    779             if self.right_index:

C:\ProgramData\Anaconda3\lib\site-packages\pandas\core\reshape\merge.py in _get_join_indexers(self)
   1449                         right_values,
   1450                         self.allow_exact_matches,
-> 1451                         tolerance)
   1452 
   1453 

TypeError: 'NoneType' object is not callable

In this post they solved by not using floats, but my numbers are already integers. I thought that maybe the strings are problematic, but then again they are used in the working example as well.

Any ideas what I'm doning wrong? Many thanks!

Upvotes: 2

Views: 1181

Answers (1)

Nev1111
Nev1111

Reputation: 1049

'col2' is not numeric

Form pandas documentation:

"on : label Field name to join on. Must be found in both DataFrames. The data MUST be ordered. Furthermore this must be a numeric column, such as datetimelike, integer, or float. On or left_on/right_on must be given."

Upvotes: 2

Related Questions