使用非均匀毫秒的日内数据同步和重新采样两个时间序列

Iva*_*van 6 python time-series pandas

我在python 文档中看到了重新采样和同步两个时间序列的能力.我的问题更难,因为时间序列没有时间规律.我读了三个具有非确定性的日内时间戳的时间序列.但是,为了对这两个时间序列进行大多数分析(协方差,相关性等),我需要它们具有相同的长度.

在Matlab中,给定了三个ts1, ts2, ts3具有非确定性日内时间戳的时间序列,我可以通过说明来同步它们

[ts1, ts2] = synchronize(ts1, ts2, 'union');
[ts1, ts3] = synchronize(ts1, ts3, 'union');
[ts2, ts3] = synchronize(ts2, ts3, 'union');
Run Code Online (Sandbox Code Playgroud)

请注意,时间序列已经读入pandas DataFrame,因此我需要能够与已创建的DataFrames同步(并重新采样?).

max*_*moo 6

根据您链接到的 Matlab 文档,听起来您想要

使用时间向量对时间序列对象重新采样,该时间向量是两个时间向量重叠的时间范围内ts1和时间向量的并集ts2

所以首先你需要找到你的数据帧索引的联合:

newindex = df1.index.union(df2.index)
Run Code Online (Sandbox Code Playgroud)

然后你可以使用这个索引重新创建你的数据帧:

df1 = df1.reindex(newindex)
df2 = df2.reindex(newindex)
Run Code Online (Sandbox Code Playgroud)

请注意,它们将NaN在所有新条目中包含s(大概这与 Matlab 中的行为相同),是否要填充这些取决于您,例如fillna(method='pad')将使用最后一个已知值填充空值,或者您可以使用interpolate(method='time')基于时间戳的线性插值。