我从两个不同的传感器收集了数据,它们以不均匀的时间间隔异步运行。我想从传感器1获得插值到传感器2的时间戳的数据。我已经找到了一种使用Pandas实现这一点的方法,首先创建一个组合的时间序列,对其进行插值,然后将插值的时间序列与第二个传感器的时间序列相结合,仅显示相交时间。有没有一种更有效率的方法来做这件事呢?下面是使用我上面描述的方法的示例代码:
import numpy as np
from matplotlib import pyplot as plt
import datetime
import pandas as pd
rand_secs = np.sort(np.random.randint(1, high=60,size=10))
times = [pd.datetime(2019, 5, 23,9, x) for x in rand_secs]
frame1 = pd.DataFrame(index = times,
data = np.sin(rand_secs/60*2*np.pi))
ax1 = frame1.plot(marker='+')
plt.xlim(pd.datetime(2019, 5, 23,9, 0), pd.datetime(2019, 5, 23,9, 59))
plt.ylim(-1.1,1.1)
times2 = [pd.datetime(2019, 5, 23,9, x) for x in np.sort(np.random.randint(1, high=60,size=10))]
frame2 = pd.DataFrame(index = times2)
frame12_combined = pd.merge(frame1, frame2, how='outer',left_index=True, right_index=True)
frame12_interp = frame12_combined.interpolate(method='index') #Linear is not Correct
frame1_resampled = pd.merge(frame2, frame12_interp, how='left',left_index=True, right_index=True)
frame1_resampled.plot(ax=ax1,style='o' )
ax1.legend(['Original time series', 'Resampled time series'])
发布于 2019-05-28 21:55:54
https://stackoverflow.com/questions/56285924
复制