2012-11-01 116 views
4

这里对齐多个时间序列的设置代码:使用重采样到大熊猫

import pandas 
from datetime import datetime 

a_values = [1728, 1635, 1733] 
a_index = [datetime(2011, 10, 31), datetime(2012, 1, 31), datetime(2012, 4, 30)] 
a = pandas.Series(data=a_values, index=a_index) 

aa_values = [6419, 5989, 6006] 
aa_index = [datetime(2011, 9, 30), datetime(2011, 12, 31), datetime(2012, 3, 31)] 
aa = pandas.Series(data=aa_values, index=aa_index) 

apol_values = [1100, 1179, 969] 
apol_index = [datetime(2011, 8, 31), datetime(2011, 11, 30), datetime(2012, 2, 29)] 
apol = pandas.Series(data=apol_values, index=apol_index) 

这里就是数据看起来像一个表(未显示对APOL 3个值):

enter image description here

目标是将数据与日历季度标记对齐,以便可以比较3个数据集。看看下面的日期,2012年3月,2011年12月和2011年9月似乎是合理的对齐标记。

下面是与fill_method = 'ffill' 输出:

In [6]: a.resample('Q', fill_method='ffill') 
Out[6]: 
2011-12-31 1728 
2012-03-31 1635 
2012-06-30 1733 
Freq: Q-DEC 

In [7]: aa.resample('Q', fill_method='ffill') 
Out[7]: 
2011-09-30 6419 
2011-12-31 5989 
2012-03-31 6006 
Freq: Q-DEC 

In [8]: apol.resample('Q', fill_method='ffill') 
Out[8]: 
2011-09-30 1100 
2011-12-31 1179 
2012-03-31  969 
Freq: Q-DEC 

,看起来像这样:

enter image description here

注意一下最近在每个系列数字不用排队。

这里与fill_method = 'bfill' 输出:

In [9]: a.resample('Q', fill_method='bfill') 
Out[9]: 
2011-12-31 1635 
2012-03-31 1733 
2012-06-30  NaN 
Freq: Q-DEC 

In [10]: aa.resample('Q', fill_method='bfill') 
Out[10]: 
2011-09-30 6419 
2011-12-31 5989 
2012-03-31 6006 
Freq: Q-DEC 

In [11]: apol.resample('Q', fill_method='bfill') 
Out[11]: 
2011-09-30 1179 
2011-12-31  969 
2012-03-31  NaN 
Freq: Q-DEC 

,看起来像这样:

enter image description here

此外,最近的一系列数字,不用排队。

这是这种情况下的预期产出resample()

我能做些什么来获得结果,其中最新的3个数字是否一致,其他所有内容是否相符?

编辑:这是所期望的输出是什么样子:

enter image description here

+0

,你能告诉我们你想要的最终输出,以确保我们了解你想达到什么。 – root

+0

只编辑了问题并添加了所需的最终输出。目标(如果不是没有说)是以编程方式得到这个,所以为某些非自动检测的方式调整每个系列的不同resample params不幸的是没有帮助 – dshap

+0

@root,我的期望输出是否有意义?这个问题有什么问题吗? (我看到有一个downvote)。非常感谢你。 – dshap

回答

5
df1 = DataFrame({'a':a}) 
df2 = DataFrame({'aa':aa}) 
df3 = DataFrame({'apol':apol}) 
df=df1.append([df2,df3]).sort_index() 
print df.resample('Q-APR',loffset='-1m').T 

输出:

 2011-09-30 2011-12-31 2012-03-31 
a   1728  1635  1733 
aa   6419  5989  6006 
apol  1100  1179   969