2014-10-07 63 views
6

假设我有一个包含多个时间戳和值的数据帧。我想每2.5秒测量Δ values/Δt。熊猫是否提供任何时间差异的工具?Pandas中的时间差异

       time_stamp values 
19492 2014-10-06 17:59:40.016000-04:00 1832128         
167106 2014-10-06 17:59:41.771000-04:00 2671048         
202511 2014-10-06 17:59:43.001000-04:00 2019434         
161457 2014-10-06 17:59:44.792000-04:00 1294051         
203944 2014-10-06 17:59:48.741000-04:00 867856 

回答

6

它当然会。首先,您需要将您的索引转换为pandas date_range格式,然后使用可用于该类的索引的系列/数据框的自定义偏移功能。有用的文档here。阅读更多关于偏移别名的here

此代码应重新取样的数据,以2.5秒的间隔

#df is your dataframe 
index = pd.date_range(df['time_stamp']) 
values = pd.Series(df.values, index=index) 

#Read above link about the different Offset Aliases, S=Seconds 
resampled_values = values.resample('2.5S') 

resampled_values.diff() #compute the difference between each point! 

应该这样做。

+0

谢谢@tyleha。我希望在上面的数据框中使用这个精确的解决方案。这是我的尝试:http://stackoverflow.com/questions/26247301/pandas-resampling-values-within-time-window-until-now我需要抽样是因果关系,但事实并非如此。 – 2014-10-08 00:28:37

+0

'df.diff().diff()'等价于找到二阶导数(近似)? – LondonRob 2016-03-24 15:06:11

+0

当然。但你最好知道你的Δt以保持你的单位有用。 2.5^2 s^2将是重击。 – tyleha 2016-03-24 21:04:48