Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

TypeError: get_loc() got an unexpected keyword argument 'method'

Tags:

python

pandas

In pandas 2 with the following code:

for time in tfo_dates:
    dt=pd.to_datetime(time)
    indx_time.append(df.index.get_loc(dt,method='nearest'))

I get this error:

TypeError: get_loc() got an unexpected keyword argument 'method'

This worked in version 1.5 but if we look at the version 2 documentation there is no method argument anymore.

What method I can use now to get nearest index of timestamp inside time index list?

like image 917
roudan Avatar asked Oct 29 '25 14:10

roudan


1 Answers

you can use get_indexer

https://pandas.pydata.org/docs/reference/api/pandas.Index.get_indexer.html

The dt (the target) argument in below call, should be an index or a list. It cannot be a single value.

df.index.get_indexer(dt,method='nearest')

Make a note that, it returns an array object.

like image 64
Pavan Chandaka Avatar answered Oct 31 '25 03:10

Pavan Chandaka



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!