TimeGrouper, pandas

匿名 (未验证) 提交于 2019-12-03 02:16:02

问题:

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) 

where monthly_return is like:

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 

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 

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    ... 

Tried the different input options (i.e. loffset) in TimeGrouper but doesn't work. Any suggestion will be really appreciated!

回答1:

The problem can be solved by adding closed = 'left'

df.groupby(pd.TimeGrouper('6M', closed = 'left')).aggregate(numpy.sum) 


回答2:

This is a workaround for what seems a bug, but give it a try and see if it works for you.

In [121]: ts = pandas.date_range('7/1/2008', periods=30, freq='MS')  In [122]: df = pandas.DataFrame(pandas.Series(range(len(ts)), index=ts))  In [124]: df[0] += 1  In [125]: df  Out[125]:               0 2008-07-01   1 2008-08-01   2 2008-09-01   3 2008-10-01   4 2008-11-01   5 2008-12-01   6 2009-01-01   7 2009-02-01   8 2009-03-01   9 2009-04-01  10 2009-05-01  11 2009-06-01  12 2009-07-01  13 2009-08-01  14 2009-09-01  15 2009-10-01  16 2009-11-01  17 2009-12-01  18 2010-01-01  19 2010-02-01  20 2010-03-01  21 2010-04-01  22 2010-05-01  23 2010-06-01  24 2010-07-01  25 2010-08-01  26 2010-09-01  27 2010-10-01  28 2010-11-01  29 2010-12-01  30 

I've used integers to help confirm that the sums are correct. The workaround that seems to work is to add a month to the front of the dataframe to trick the TimeGrouper into doing what you need.

In [127]: df2 = pandas.DataFrame([0], index = [df.index.shift(-1, freq='MS')[0]])  In [129]: df2.append(df).groupby(pandas.TimeGrouper(freq='6M')).aggregate(numpy.sum)[1:] Out[129]:                0 2008-12-31   21 2009-06-30   57 2009-12-31   93 2010-06-30  129 2010-12-31  165 

Note the final [1:] is there to trim off the first group.



回答3:

TimeGrouper that is suggested in other answers is deprecated and will be removed from Pandas. It is replaced with Grouper. So a solution to your question using Grouper is:

df.groupby(pd.Grouper(freq='6M', closed='left')).aggregate(numpy.sum) 


文章来源: TimeGrouper, pandas
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!