Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pandas interpolate throws an invalid fill method error after version 0.24

Tags:

python

pandas

The pandas.DataFrame.interpolate() method is throwing this error when using the method='time' argument:

Error: Invalid fill method. Expecting pad (ffill) or backfill (bfill). Got time

This only started happening after pandas updated to version 0.24, which seems to have changed some implementation. Code that used to work breads with version 0.24 or greater.

I thought it might have to do with the data types of the columns, meaning that if the DataFrame included non-numeric columns the interpolation would fail. That doesn't seems to be the case though, since I've recently run into this again with a purely numeric DataFrame.

I believe I got this to work at some point, but I can't remember how (other than messing with data types). This is problematic because my existing codebase has to pin pandas to 0.23 for the functionality to work.

like image 602
Lucas Lofaro Avatar asked Nov 01 '25 05:11

Lucas Lofaro


1 Answers

For anyone who has a similar issue, I also got this error due to the new pandas types after 1.1.x.

For me, the problem was that I was trying to interpolate data that was an Int64 type and it would only let me do a forward or backward fill. In previous versions of pandas this wasn't a problem as the type automatically be recast to float to complete the interpolation.

This is not the case anymore -- you need to explicitly cast to float and then you can interpolate. Yes, it is completely unnecessary type safety. Yes, it is a horrible error message.

like image 130
Zach Avatar answered Nov 02 '25 19:11

Zach



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!