繁体   English   中英

合并并采样两个熊猫时间序列

[英]merge and sample two pandas time series

我有两个时间序列。 我想将它们合并并asfreq(*, method='pad')结果,限于它们共同共享的时间范围。

为了说明这一点,假设我这样定义AB

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

我怎样才能做到这一点?

我不确定您要问的是什么,但这是一个有点复杂的方法,该方法首先找到重叠时间,然后创建一个单列数据帧作为5s timedelta的“基本”数据帧。

通过正确设置数据帧开始

start = max(A.index.min(), B.index.min())
end = min(A.index.max(), B.index.max())

df_time = pd.DataFrame({'time': pd.date_range(start,end,freq='5s')})

df_A = A.reset_index()
df_B = B.reset_index()

df_A.columns = ['time', 'value']
df_B.columns = ['time', 'value']

现在我们有以下三个数据框。

DF_A

                 time  value
0 2017-01-04 10:00:00      0
1 2017-01-04 10:00:10      1
2 2017-01-04 10:00:20      2
3 2017-01-04 10:00:30      3

DF_B

                time  value
0 2017-01-04 10:00:07      0
1 2017-01-04 10:00:10      1
2 2017-01-04 10:00:13      2
3 2017-01-04 10:00:16      3
4 2017-01-04 10:00:19      4
5 2017-01-04 10:00:22      5

df_time

                 time
0 2017-01-04 10:00:07
1 2017-01-04 10:00:12
2 2017-01-04 10:00:17
3 2017-01-04 10:00:22

使用merge_asof加入所有三个

pd.merge_asof(pd.merge_asof(df_time, df_A, on='time'), df_B, on='time', suffixes=('_A', '_B'))


                 time  value_A  value_B
0 2017-01-04 10:00:07        0        0
1 2017-01-04 10:00:12        1        1
2 2017-01-04 10:00:17        1        3
3 2017-01-04 10:00:22        2        5

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM