btk
btk

Reputation: 81

Pandas interpolate until end of series

I have something like this:

DateTime             Col1     
2016-10-01 00:00:00  1.000
2016-10-01 01:00:00  2.000
2016-10-01 02:00:00  NaN
2016-10-01 03:00:00  4.000
2016-10-01 04:00:00  5.000
2016-10-01 05:00:00  NaN
2016-10-01 06:00:00  NaN

When I use df.interpolate(method='time'), the NaN values at the end are set to the same value as the latest before, meaning I get:

DateTime             Col1     
2016-10-01 00:00:00  1.000
2016-10-01 01:00:00  2.000
2016-10-01 02:00:00  3.000
2016-10-01 03:00:00  4.000
2016-10-01 04:00:00  5.000
2016-10-01 05:00:00  5.000
2016-10-01 06:00:00  5.000

What I actually want is for the interpolation to continue until the end:

DateTime             Col1     
2016-10-01 00:00:00  1.000
2016-10-01 01:00:00  2.000
2016-10-01 02:00:00  3.000
2016-10-01 03:00:00  4.000
2016-10-01 04:00:00  5.000
2016-10-01 05:00:00  6.000
2016-10-01 06:00:00  7.000

Is this possible without making my own interpolation function?

Upvotes: 3

Views: 362

Answers (1)

mozway
mozway

Reputation: 260640

You can't extrapolate data with method='time'.

You could use method='spline':

df.interpolate(method='spline', order=1)

output:

                     Col1
DateTime                 
2016-10-01 00:00:00   1.0
2016-10-01 01:00:00   2.0
2016-10-01 02:00:00   3.0
2016-10-01 03:00:00   4.0
2016-10-01 04:00:00   5.0
2016-10-01 05:00:00   6.0
2016-10-01 06:00:00   7.0

Another approach if you want to combine two interpolation methods:

(df.interpolate(method='time', limit_area='inside')
   .fillna(df.interpolate(method='spline', order=1))
)

Upvotes: 3

Related Questions