2015-06-09 57 views
3

我有两个日常系列,我必须将它们合并为一个小时系列,第一系列为前12小时,第二系列为剩余小时。将两个日常系列合并为一个小时系列

有没有更有效的方法,而不是手动建立一个列表并将其转换为系列?由于

a = pd.Series(np.random.rand(5), pd.date_range('2015-01-01', periods=5)) 
b = pd.Series(np.random.rand(5), pd.date_range('2015-01-01', periods=5)) 
c = an hourly series 
+0

也许如果你用非随机数据和你想要的输出展示一个例子,可能会更容易推断你的需求 – holdenweb

回答

1

可能:

>>> b.index += dt.timedelta(hours=12) 
>>> pd.concat((a, b), axis=0).sort_index() 
2015-01-01 00:00:00 0.150 
2015-01-01 12:00:00 0.970 
2015-01-02 00:00:00 0.746 
2015-01-02 12:00:00 0.937 
2015-01-03 00:00:00 0.523 
2015-01-03 12:00:00 0.392 
2015-01-04 00:00:00 0.606 
2015-01-04 12:00:00 0.459 
2015-01-05 00:00:00 0.221 
2015-01-05 12:00:00 0.029 
dtype: float64 

,并ts.asfreq('H', method='ffill')有每小时频率。

+0

谢谢,但实际上我的意思是 - 结果系列应该是每小时系列,即每天24个数据点,对于前12个小时,我们使用系列a的数据,下半部分使用系列b的数据 – John

+0

@John您可以对最后一个值 –

+0

看起来像'ts.asfreq('H',method ='ffill')'第一行不行?出现以下错误 TypeError:ufunc add不能使用类型为dtype(' John