1

I have a dataframe df that contains data in periods of 3 hours:

index              , values
2003-01-01 00:00:00, 2.0    
2003-01-01 03:00:00, 1.8    
2003-01-01 06:00:00, 1.4    
2003-01-01 09:00:00, 1.1
....    

I want to resample the data to every hour and interpolate the missing values in between linearly. I can achieve something similar, filling the missing values with .bfill(), and it looks like this:

df2 = df.resample('H').bfill()

I tried to alter this to achive my task as follows:

df2 = df.resample('H')
df2.interpolate(method='linear', axis=0, inplace=True)

But df2 = df.resample('H') in contrast to df2 = df.resample('H').bfill() doesn't return a dataframe object, but a pandas.core.resample.DatetimeIndexResampler object.

Do you know how I can do the resampling and interpolation? Do you have some other work around? Tnx

NeStack
  • 1,739
  • 1
  • 20
  • 40
  • 1
    From the docs: _resample() is a time-based groupby, followed by a reduction method on each of its groups. See some cookbook examples for some advanced strategies_. With this in mind, I think [this](https://stackoverflow.com/questions/37057187/pandas-interpolate-within-a-groupby) answer might help you – gionni Feb 18 '21 at 16:51
  • @gionni Thanks, this helped! I actually found out from that I can just append my approach with `.interpolate()` and it would do the job – NeStack Feb 18 '21 at 17:05

1 Answers1

1

I found out, that I could just append my initial approach with .interpolate() and it would work:

df2 = df.resample('H').interpolate()
df2.interpolate(method='linear', axis=0, inplace=True)
NeStack
  • 1,739
  • 1
  • 20
  • 40