使用Pandas .diff()从时间序列计算二阶导数 [英] Calculating second order derivative from timeseries using Pandas .diff()
问题描述
下面是我的Pandas数据框的示例,以日期作为索引:
An example of my Pandas dataframe is below, with dates as index:
A
2019-01-09 1.007042
2019-01-10 1.007465
2019-01-11 1.007054
2019-01-12 1.007154
2019-01-13 1.007854
2019-01-14 1.008560
我想每两列A列确定二阶导数。是否适合两次使用Pandas中的 .diff()
函数进行此确定?
I'd like to determine the second order derivative every 2 rows of column A. Would it be appropriate to use the .diff()
function in Pandas twice for this determination?
df['A2'] = df['A'].diff(2).diff(2)
推荐答案
这是一个长达9个月的问题,但仍显示在google搜索此材料的顶部,因此值得接受。如果不需要此免责声明,我希望有人为我删除它。
This is a nine month old question, but still appears at the top of google searches for this material and so warrants an acceptable answer. If this disclaimer is unnecessary, I hope someone will remove it for me.
二阶导数可以作为中心,前向或后向导数进行计算,但要基于您的例如,我认为您正在寻找向后导数。可以根据需要在每一行上进行计算,但是,使用 diff()
确实很难。函数 shift()
可以很好地工作,其方法如下:
The second derivative can be calculated either as a central, forward or backward derivative, but based off your example, I think you're looking for the backward derivative. It can be calculated on every row if you want, however, it could be really hard to do with diff()
. The function shift()
works well though and the method is as follows:
df['A2'] = df['A'] - 2*df['A'].shift(1) + df['A'].shift(2)
该技术依赖于有限差异
这篇关于使用Pandas .diff()从时间序列计算二阶导数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!