pandas TimeGrouper,熊猫
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14569223/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
TimeGrouper, pandas
提问by user2019264
I use TimeGrouper
from pandas.tseries.resample
to sum monthly return to 6M as follows:
我使用TimeGrouper
frompandas.tseries.resample
将月回报总结为 6M 如下:
6m_return = monthly_return.groupby(TimeGrouper(freq='6M')).aggregate(numpy.sum)
where monthly_return
is like:
哪里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
The 6m_return is like:
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
However I want to get the 6m_return
starting 6m from 7/2008 like the following:
但是,我想6m_return
从 7/2008获得起始 6m,如下所示:
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!
在 TimeGrouper 中尝试了不同的输入选项(即 loffset),但不起作用。任何建议将不胜感激!
回答by Anja
The problem can be solved by adding closed = 'left'
问题可以通过添加closed = 'left'来解决
df.groupby(pd.TimeGrouper('6M', closed = 'left')).aggregate(numpy.sum)
回答by Primoz
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:
TimeGrouper
不推荐使用其他答案中建议的内容,并将从Pandas
. 它被替换为Grouper
。因此,使用您的问题的解决方案Grouper
是:
df.groupby(pd.Grouper(freq='6M', closed='left')).aggregate(numpy.sum)
回答by D. A.
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.
我使用整数来帮助确认总和是正确的。似乎有效的解决方法是在数据框的前面添加一个月以欺骗 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
Note the final [1:]
is there to trim off the first group.
请注意,决赛[1:]
是为了修剪第一组。