简体   繁体   English

pandas groupby 闰年失败

[英]pandas groupby with leap year fails

I want to plot different graphs on my time series data.我想在我的时间序列数据上使用 plot 不同的图表。

My problem is that it fails when I include a year with a leap year:我的问题是当我包含闰年的年份时它会失败:

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()

Output: Output:

            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

You can do concat:你可以做连接:

groups = df.groupby(pd.Grouper(freq='A')),

years = pd.concat([pd.Series(x.values.flatten(), name=y) 
                   for y,x in groups],
                  axis=1)

years.boxplot()

Output: Output:

在此处输入图像描述

which gives (notice the xtick labels):这给出了(注意 xtick 标签):

在此处输入图像描述

However, I would do instead of using Grouper但是,我会做而不是使用Grouper

groups = df.groupby(df.index.year)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM