我正在尝试合并两个系列,一个小时,另一个分钟(并将索引保持在分钟级别):
Series A:
time
2017-09-01 01:00:00 0.5
2017-09-01 02:00:00 0.4
Freq: H, Name: A, dtype: float64
系列B分钟:
Series B
time
2017-09-01 00:00:00 NaN
2017-09-01 00:03:00 -0.000350
2017-09-01 00:06:00 0.000401
Name: B, dtype: float64
我想按小时合并这两个系列。所需的合并数据框将是:
time
2017-09-01 00:00:00 NaN 0.5
2017-09-01 00:03:00 -0.000350 0.5
2017-09-01 00:06:00 0.000401 0.5
2017-09-01 01:00:00 0.002301 0.4
2017-09-01 01:03:00 0.005601 0.4
搜索SO之后,我找到的一种方法是创建一个小时' B系列中的列,然后使用A的索引和'小时'加入A到B. B栏:(参考:Merging/combining two dataframes with different frequency time series indexes in Pandas?)
B = B.to_frame()
B['hour'] = B.index.to_period('H')
merged = B.join(A, on = 'hour', how = 'left')
但是我收到了错误消息:
TypeError: Argument 'values' has incorrect type (expected numpy.ndarray, got Index)
有谁知道如何解决这个问题?或者也许有更好的方式加入这两个系列?非常感谢!
答案 0 :(得分:1)
演示:
In [280]: A
Out[280]:
time
2017-09-01 01:00:00 0.5
2017-09-01 02:00:00 0.4
Name: val, dtype: float64
In [281]: B
Out[281]:
time
2017-09-01 00:00:00 NaN
2017-09-01 00:03:00 -0.000350
2017-09-01 00:06:00 0.000401
Name: val, dtype: float64
In [282]: B.to_frame('B').join(A.to_frame('A').set_index(A.index.shift(-1, freq='H')).resample('3T').ffill())
Out[282]:
B A
time
2017-09-01 00:00:00 NaN 0.5
2017-09-01 00:03:00 -0.000350 0.5
2017-09-01 00:06:00 0.000401 0.5