【发布时间】:2018-08-29 11:52:19
【问题描述】:
我想计算此 Dataframe test 中每一行的 1 年滚动平均值:
index id date variation
2313 7034 2018-03-14 4.139148e-06
2314 7034 2018-03-13 4.953194e-07
2315 7034 2018-03-12 2.854749e-06
2316 7034 2018-03-09 3.907458e-06
2317 7034 2018-03-08 1.662412e-06
2318 7034 2018-03-07 1.346433e-06
2319 7034 2018-03-06 8.731700e-06
2320 7034 2018-03-05 7.145597e-06
2321 7034 2018-03-02 4.893283e-06
...
例如,我需要计算:
- id
7034在 2018-03-14 和 2017-08-14 之间的平均变化 - id
7034在 2018-03-13 和 2017-08-13 之间的平均变化 - 等
我试过了:
test.groupby(['id','date'])['variation'].rolling(window=1,freq='Y',on='date').mean()
但我收到了错误消息:
ValueError: invalid on specified as date, must be a column (if DataFrame) or None
在这种情况下如何使用 pandas rolling() 函数?
[编辑 1] [感谢 Sacul]
我测试过:
df['date'] = pd.to_datetime(df['date'])
df.set_index('date').groupby('id').rolling(window=1, freq='Y').mean()['variation']
但是freq='Y' 不起作用(我得到:ValueError: Invalid frequency: Y)然后我使用了window = 365, freq = 'D'。
但还有另一个问题:因为每个组合 id-date 永远不会有 365 个连续日期,因此结果始终为空。即使缺少日期,我也想忽略它们并考虑当前日期和(当前日期 - 365)之间的所有日期来计算滚动平均值。例如,假设我有:
index id date variation
2313 7034 2018-03-14 4.139148e-06
2314 7034 2018-03-13 4.953194e-07
2315 7034 2017-03-13 2.854749e-06
那么,
- 对于 7034 2018-03-14:我想计算 MEAN(4.139148e-06,4.953194e-07, 2.854749e-06)
- 对于 7034 2018-03-13:我还想计算 MEAN(4.139148e-06,4.953194e-07, 2.854749e-06)
我该怎么做?
[编辑 2]
最后我使用下面的公式通过忽略缺失值来计算 1 年的滚动中位数、平均值和标准差:
pd.rolling_median(df.set_index('date').groupby('id')['variation'],window=365, freq='D',min_periods=1)
pd.rolling_mean(df.set_index('date').groupby('id')['variation'],window=365, freq='D',min_periods=1)
pd.rolling_std(df.set_index('date').groupby('id')['variation'],window=365, freq='D',min_periods=1)
【问题讨论】:
-
格式正确,显示尝试,明确给出错误。欢迎使用 stackoverflow!
标签: python pandas rolling-computation rolling-average