Home > Software design >  How to interpolate by copying values?
How to interpolate by copying values?

Time:09-29

I have hourly data and I need to interpolate it to 10 minutes data by simply repeating the values:

data=[('2014-02-24 16:00:00', 55)
,('2014-02-24 17:00:00', 40)
,('2014-02-24 18:00:00', 68)]

df=pd.DataFrame(data,columns=['DateTime','Value'])

I need to get the following result:

DateTime                  Value
2014-02-24  16:00:00      55
2014-02-24  16:10:00      55
2014-02-24  16:20:00      55
...
2014-02-24  17:00:00      40
2014-02-24  17:10:00      40
2014-02-24  17:20:00      40
...

I know that it's possible to resample data in this way minutes=df.resample('10Min',on='DateTime').mean().

But how can I do the simple interpolation shown above?

CodePudding user response:

Use Resampler.ffill working with DatetimeIndex:

df['DateTime'] = pd.to_datetime(df['DateTime'])

minutes=df.set_index('DateTime').resample('10Min').ffill()

print (minutes)
                     Value
DateTime                  
2014-02-24 16:00:00     55
2014-02-24 16:10:00     55
2014-02-24 16:20:00     55
2014-02-24 16:30:00     55
2014-02-24 16:40:00     55
2014-02-24 16:50:00     55
2014-02-24 17:00:00     40
2014-02-24 17:10:00     40
2014-02-24 17:20:00     40
2014-02-24 17:30:00     40
2014-02-24 17:40:00     40
2014-02-24 17:50:00     40
2014-02-24 18:00:00     68
  • Related