23

我使用TimeGrouperfrompandas.tseries.resample来总结每月回报到 6M,如下所示:

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 就像:

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

但是我想6m_return从 7/2008 开始 6m,如下所示:

2008-12-31    ...
2009-06-31    ...
2009-12-31    ...
2010-06-31    ...
2010-12-31    ...

在 TimeGrouper 中尝试了不同的输入选项(即 loffset)但不起作用。任何建议将不胜感激!

4

3 回答 3

8

问题可以通过添加 closed = 'left' 来解决

df.groupby(pd.TimeGrouper('6M', closed = 'left')).aggregate(numpy.sum)
于 2014-03-20T09:05:22.900 回答
3

TimeGrouper其他答案中建议的内容已弃用,将从Pandas. 它被替换为Grouper。因此,您的问题的解决方案Grouper是:

df.groupby(pd.Grouper(freq='6M', closed='left')).aggregate(numpy.sum)
于 2018-02-15T20:12:08.290 回答
2

这是解决似乎是错误的解决方法,但请尝试一下,看看它是否适合您。

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

我使用整数来帮助确认总和是否正确。似乎可行的解决方法是在数据框的前面添加一个月,以诱使 TimeGrouper 执行您需要的操作。

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

请注意,决赛[1:]是为了修剪第一组。

于 2013-02-26T15:06:12.010 回答