python - 闰年的熊猫 groupby 失败
问题描述
我想在我的时间序列数据上绘制不同的图表。
我的问题是当我包含闰年的年份时它会失败:
groups = daily_incidents_df.groupby(Grouper(freq='A'))
years = pd.DataFrame()
for name, group in groups:
print(group)
years[name.year] = group.values.squeeze()
years.boxplot()
plt.show()
输出:
num_incidents
date
2015-01-01 175
2015-01-02 84
2015-01-03 94
2015-01-04 90
2015-01-05 78
... ...
2015-12-27 138
2015-12-28 113
2015-12-29 103
2015-12-30 90
2015-12-31 110
[365 rows x 1 columns]
num_incidents
date
2016-01-01 183
2016-01-02 110
2016-01-03 134
2016-01-04 105
2016-01-05 102
... ...
2016-12-27 135
2016-12-28 134
2016-12-29 145
2016-12-30 111
2016-12-31 159
[366 rows x 1 columns]
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-17-6eb0a1a15c64> in <module>()
3 for name, group in groups:
4 print(group)
----> 5 years[name.year] = group.values.squeeze()
6 years.boxplot()
7 plt.show()
3 frames
/usr/local/lib/python3.6/dist-packages/pandas/core/internals/construction.py in sanitize_index(data, index, copy)
609
610 if len(data) != len(index):
--> 611 raise ValueError("Length of values does not match length of index")
612
613 if isinstance(data, ABCIndexClass) and not copy:
ValueError: Length of values does not match length of index
解决方案
推荐阅读
- c# - DataTemplate 中的 WPF 绑定用户控件
- c++ - 从文件中提取的字符串未正确计算
- c++ - 如何在 C++ 中优化数组的交换元素
- load-balancing - 负载均衡器如何处理所有服务器都忙的情况?
- javascript - 无论地图视图/缩放如何,如何使用 Google Maps API 一次只能看到 10 个标记
- xamarin.forms - 如何在 Xamarin.Forms 中以编程方式调用 Binding.Path?
- java - XML 布局未出现在 R.layout 中
- api - 通过 GitHub API 获取所有私有仓库的名称
- botframework - 有没有一种干净的方式让机器人使用 Azure BotFramework 回复电子邮件线程?
- r - 应用 walk.forward 函数时出现 quantstrat 错误