【发布时间】:2017-01-05 20:45:15
【问题描述】:
我有两个时间序列。我想合并它们和asfreq(*, method='pad') 结果,限制在它们共同共享的时间范围内。
为了说明,假设我这样定义A 和B:
import datetime as dt
import numpy as np
import pandas as pd
A = pd.Series(np.arange(4), index=pd.date_range(dt.datetime(2017,1,4,10,0,0),
periods=4, freq=dt.timedelta(seconds=10)))
B = pd.Series(np.arange(6), index=pd.date_range(dt.datetime(2017,1,4,10,0,7),
periods=6, freq=dt.timedelta(seconds=3)))
所以它们看起来像:
# A
2017-01-04 10:00:00 0
2017-01-04 10:00:10 1
2017-01-04 10:00:20 2
2017-01-04 10:00:30 3
# B
2017-01-04 10:00:07 0
2017-01-04 10:00:10 1
2017-01-04 10:00:13 2
2017-01-04 10:00:16 3
2017-01-04 10:00:19 4
2017-01-04 10:00:22 5
我想计算如下:
# combine_and_asfreq(A, B, dt.timedelta(seconds=5))
# timestamp A B
2017-01-04 10:00:07 0 0
2017-01-04 10:00:12 1 1
2017-01-04 10:00:17 1 3
2017-01-04 10:00:22 2 5
我该怎么做?
【问题讨论】:
-
你可以看看函数merge_asof。我认为这就是应该做的。如果这不起作用,您可以进行外部连接,重新采样,然后删除剩余的 nan 值。 pandas.pydata.org/pandas-docs/stable/generated/…
-
您是如何获得最终数据框中的时间的。为什么它们间隔 5 秒?
-
@TedPetrou 刚刚编辑了帖子以包含一个带有
timedelta(seconds=5)参数的函数调用。