使用时间索引在 Pandas 数据帧上滚动积分 [英] Rolling integral over pandas dataframe with time index

查看:34
本文介绍了使用时间索引在 Pandas 数据帧上滚动积分的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想用时间索引捕获我的数据帧列的积分.这适用于每个时间间隔发生的分组.

from scipy import 集成>>>df时间 A2017-12-18 19:54:40 -50187.02017-12-18 19:54:45 -60890.52017-12-18 19:54:50 -28258.52017-12-18 19:54:55 -8151.02017-12-18 19:55:00 -9108.52017-12-18 19:55:05 -12047.02017-12-18 19:55:10 -19418.02017-12-18 19:55:15 -50686.02017-12-18 19:55:20 -57159.02017-12-18 19:55:25 -42847.0>>>积分 df = df.groupby(pd.Grouper(freq='25S')).apply(integrate.trapz)时间 A2017-12-18 19:54:35 -118318.002017-12-18 19:55:00 -115284.752017-12-18 19:55:25 0.00频率:25S,名称:A,数据类型:float64

scipy 积分函数自动使用时间索引来计算其结果.

这不是真的.您必须明确地将转换传递给 np datetime,以便 scipy.integrate.trapz 使用时间正确集成.请参阅我对这个问题的评论.

但是,我想改用滚动积分.我试过

I want to capture an integral of a column of my dataframe with a time index. This works fine for a grouping that happens every time interval.

from scipy import integrate
>>> df

Time                      A
2017-12-18 19:54:40   -50187.0
2017-12-18 19:54:45   -60890.5
2017-12-18 19:54:50   -28258.5
2017-12-18 19:54:55    -8151.0
2017-12-18 19:55:00    -9108.5
2017-12-18 19:55:05   -12047.0
2017-12-18 19:55:10   -19418.0
2017-12-18 19:55:15   -50686.0
2017-12-18 19:55:20   -57159.0
2017-12-18 19:55:25   -42847.0

>>> integral_df = df.groupby(pd.Grouper(freq='25S')).apply(integrate.trapz)

Time                       A
2017-12-18 19:54:35   -118318.00
2017-12-18 19:55:00   -115284.75
2017-12-18 19:55:25         0.00
Freq: 25S, Name: A, dtype: float64

EDIT:

The scipy integral function automatically uses the time index to calculate it's result.

This is not true. You have to explicitly pass the conversion to np datetime in order for scipy.integrate.trapz to properly integrate using time. See my comment on this question.

But, i'd like to take a rolling integral instead. I've tried Using rolling functions found on SO, But the code was getting messy as I tried to workout my input to the integrate function, as these rolling functions don't return dataframes.

How can I take a rolling integral over time over a function of one of my dataframe columns?

解决方案

set the Time as index, and use apply with df.rolling

df.Time = pd.to_datetime(df.Time)
df = df.set_index('Time')
df.rolling('25S').apply(integrate.trapz)

#outputs:

2017-12-18 19:54:40         0.00
2017-12-18 19:54:45    -55538.75
2017-12-18 19:54:50   -100113.25
2017-12-18 19:54:55   -118318.00
2017-12-18 19:55:00   -126947.75
2017-12-18 19:55:05    -81986.75
2017-12-18 19:55:10    -53144.75
2017-12-18 19:55:15    -69992.00
2017-12-18 19:55:20   -115284.75
2017-12-18 19:55:25   -154710.00

Here is a plot of A & A-rolling-integrated:

这篇关于使用时间索引在 Pandas 数据帧上滚动积分的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆