Pandas 按滚动窗口聚合组-键错误:';未找到列:foo';
原始数据集:Pandas 按滚动窗口聚合组-键错误:';未找到列:foo';,pandas,pandas-groupby,Pandas,Pandas Groupby,原始数据集: index = pd.MultiIndex.from_product([['AAA','BBB'], pd.DatetimeIndex(['2017-08-17', '2017-08-20', '2017-09-08'])]) df = pd.DataFrame(data=[[1.0], [3.0], [5.0], [7.0], [9.0], [11.0]], index=index, columns=['foo']) 将数据帧重新索引为abble,以使用居中滚动窗口(使索引一致
index = pd.MultiIndex.from_product([['AAA','BBB'], pd.DatetimeIndex(['2017-08-17', '2017-08-20', '2017-09-08'])])
df = pd.DataFrame(data=[[1.0], [3.0], [5.0], [7.0], [9.0], [11.0]], index=index, columns=['foo'])
将数据帧重新索引为abble,以使用居中滚动窗口(使索引一致)
以下代码按预期工作
df['foo'].groupby(level=0, group_keys=False).rolling(7, min_periods=1, center=True).mean().head(10)
AAA 2017-08-15 1.0
2017-08-16 1.0
2017-08-17 2.0
2017-08-18 2.0
2017-08-19 2.0
2017-08-20 2.0
2017-08-21 3.0
2017-08-22 3.0
2017-08-23 3.0
2017-08-24 NaN
Name: foo, dtype: float64
但是,如果我尝试使用聚合,就会产生错误
df['foo'].groupby(level=0, group_keys=False).rolling(7, min_periods=1, center=True).agg(['mean', 'count'])
KeyError: 'Column not found: foo'
Stacktrace:pd.\uuuuuuu版本为“0.20.3”,您可能需要检查
df['foo'].groupby(level=0, group_keys=False).rolling(7, min_periods=1, center=True).agg(['mean', 'count'])
KeyError: 'Column not found: foo'