Pandas 中的时间差异

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/26245242/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 22:33:19  来源:igfitidea点击:

Time differentiation in Pandas

pythonnumpypandas

提问by Amelio Vazquez-Reina

Say I have a dataframe with several timestamps and values. I would like to measure Δ values / Δtevery 2.5seconds. Does Pandas provide any utilities for time differentiation?

假设我有一个带有多个时间戳和值的数据框。我想测量Δ values / Δt每一2.5秒。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

采纳答案by tyleha

It most certainly does. First, you'll need to convert your indices into pandas date_rangeformat and then use the custom offset functions available to series/dataframes indexed with that class. Helpful documentation here. Read more hereabout offset aliases.

它肯定会。首先,您需要将索引转换为 Pandasdate_range格式,然后使用可用于用该类索引的系列/数据帧的自定义偏移函数。有用的文档在这里在此处阅读有关偏移别名的更多信息。

This code should resample your data to 2.5s intervals

此代码应将您的数据重新采样为 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!

That should do it.

那应该这样做。

回答by ViggoTW

If you really want the time derivative, then you also need to divide by the time difference (delta time, dt) since last sample

如果你真的想要时间导数,那么你还需要除以自上次采样以来的时间差(delta time, dt

An example:

一个例子:

dti = pd.DatetimeIndex([
    '2018-01-01 00:00:00',
    '2018-01-01 00:00:02',
    '2018-01-01 00:00:03'])

X = pd.DataFrame({'data': [1,3,4]}, index=dti)

X.head()
                    data
2018-01-01 00:00:00 1
2018-01-01 00:00:02 3
2018-01-01 00:00:03 4

You can find the time delta by using the diff()on the DatetimeIndex. This gives you a series of type Time Deltas. You only need the values in seconds, though

您可以使用diff()DatetimeIndex 上的找到时间增量。这为您提供了一系列类型的时间增量。不过,您只需要几秒钟的值

dt = pd.Series(df.index).diff().dt.seconds.values

dXdt = df.diff().div(dt, axis=0, )

dXdt.head()
                    data
2018-01-01 00:00:00 NaN
2018-01-01 00:00:02 1.0
2018-01-01 00:00:03 1.0

As you can see, this approach takes into account that there are two seconds between the first two values, and only one between the two last values. :)

如您所见,这种方法考虑到前两个值之间有两秒,而最后两个值之间只有一秒。:)