1 回答
TA贡献1797条经验 获得超6个赞
方法1:as.freq
new_df = df.set_index('Time').asfreq('15min', fill_value=0).reset_index()
new_df['delta time'] = new_df.Time.diff()
>>> new_df
Time Value delta time
0 2009-05-27 18:45:00 2 NaT
1 2009-05-27 19:00:00 4 00:15:00
2 2009-05-27 19:15:00 1 00:15:00
3 2009-05-27 19:30:00 1 00:15:00
4 2009-05-27 19:45:00 0 00:15:00
5 2009-05-27 20:00:00 0 00:15:00
6 2009-05-27 20:15:00 0 00:15:00
7 2009-05-27 20:30:00 0 00:15:00
8 2009-05-27 20:45:00 2 00:15:00
方法2:resample
new_df= (df.resample('15min', on='Time').mean()
.fillna(0)
.reset_index()
.assign(delta_time = lambda x: x.Time.diff()))
>>> new_df
Time Value delta_time
0 2009-05-27 18:45:00 2.0 NaT
1 2009-05-27 19:00:00 4.0 00:15:00
2 2009-05-27 19:15:00 1.0 00:15:00
3 2009-05-27 19:30:00 1.0 00:15:00
4 2009-05-27 19:45:00 0.0 00:15:00
5 2009-05-27 20:00:00 0.0 00:15:00
6 2009-05-27 20:15:00 0.0 00:15:00
7 2009-05-27 20:30:00 0.0 00:15:00
8 2009-05-27 20:45:00 2.0 00:15:00
添加回答
举报