本文介绍了TimeGrouper,大 pandas 的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我使用 TimeGrouper
从 pandas.tseries.resample
将每月返回为6M,如下所示:
I use TimeGrouper
from pandas.tseries.resample
to sum monthly return to 6M as follows:
6m_return = monthly_return.groupby(TimeGrouper(freq='6M')).aggregate(numpy.sum)
其中 monthly_return
就像:
2008-07-01 0.003626
2008-08-01 0.001373
2008-09-01 0.040192
2008-10-01 0.027794
2008-11-01 0.012590
2008-12-01 0.026394
2009-01-01 0.008564
2009-02-01 0.007714
2009-03-01 -0.019727
2009-04-01 0.008888
2009-05-01 0.039801
2009-06-01 0.010042
2009-07-01 0.020971
2009-08-01 0.011926
2009-09-01 0.024998
2009-10-01 0.005213
2009-11-01 0.016804
2009-12-01 0.020724
2010-01-01 0.006322
2010-02-01 0.008971
2010-03-01 0.003911
2010-04-01 0.013928
2010-05-01 0.004640
2010-06-01 0.000744
2010-07-01 0.004697
2010-08-01 0.002553
2010-09-01 0.002770
2010-10-01 0.002834
2010-11-01 0.002157
2010-12-01 0.001034
6m_return如下:
The 6m_return is like:
2008-07-31 0.003626
2009-01-31 0.116907
2009-07-31 0.067688
2010-01-31 0.085986
2010-07-31 0.036890
2011-01-31 0.015283
但是,我想从7/2008开始,从6/2008开始的 6m_return
,如下所示:
However I want to get the 6m_return
starting 6m from 7/2008 like the following:
2008-12-31 ...
2009-06-31 ...
2009-12-31 ...
2010-06-31 ...
2010-12-31 ...
尝试了不同的输入选项loffset)在TimeGrouper,但不起作用。
任何建议将非常感激!
Tried the different input options (i.e. loffset) in TimeGrouper but doesn't work.Any suggestion will be really appreciated!
推荐答案
问题可以通过添加closed ='left' p>
The problem can be solved by adding closed = 'left'
df.groupby(pd.TimeGrouper('6M', closed = 'left')).aggregate(numpy.sum)
这篇关于TimeGrouper,大 pandas 的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!