Home > Blockchain >  Pandas resample interpolate behavior is odd
Pandas resample interpolate behavior is odd

Time:11-01

When asking pandas to resample this dataframe using interpolate it fails to do so properly simply propagating the first value forwards. Can someone explain this behavior? Using a recent version of pandas and using python 3.8

import pandas as pd
data = {'time': pd.to_timedelta([0., 1.1, 2.2, 3.3, 4.4], unit='s'), 'data':[100, 140, 50, 300, 400]}
df1 = pd.DataFrame(data=data).set_index('time')
df2 = df1.resample('1s').interpolate()
print(df1)
print(df2)
                        data
time                        
0 days 00:00:00          100
0 days 00:00:01.100000   140
0 days 00:00:02.200000    50
0 days 00:00:03.300000   300
0 days 00:00:04.400000   400
                  data
time                  
0 days 00:00:00  100.0
0 days 00:00:01  100.0
0 days 00:00:02  100.0
0 days 00:00:03  100.0
0 days 00:00:04  100.0

CodePudding user response:

You need to first resample to a smaller unit and interpolate to get intermediate values. Then resample to seconds to get the data with equidistant timesteps.

df1.resample('ms').interpolate().resample('s').asfreq()
                       data
time                       
0 days 00:00:00  100.000000
0 days 00:00:01  136.363636
0 days 00:00:02   66.363636
0 days 00:00:03  231.818182
0 days 00:00:04  363.636364
  • Related