Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/358.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python Pandas使用其他不规则时间列表对不规则时间序列进行重采样和插值_Python_Pandas_Time Series_Interpolation_Resampling - Fatal编程技术网

Python Pandas使用其他不规则时间列表对不规则时间序列进行重采样和插值

Python Pandas使用其他不规则时间列表对不规则时间序列进行重采样和插值,python,pandas,time-series,interpolation,resampling,Python,Pandas,Time Series,Interpolation,Resampling,我从两个不同的传感器收集数据,它们以不均匀的间隔异步运行。我想从传感器1获取数据,并将其插入传感器2的时间戳中。 我找到了一种处理熊猫的方法,首先创建一个组合时间序列,对其进行插值,然后将插值后的时间序列与第二个传感器的时间序列相结合,只显示相交时间。 有没有一种更具Pythonic(或Pandaic)的方法来更有效地实现这一点。下面是一个示例代码,它使用了我上面描述的方法: import numpy as np from matplotlib import pyplot as plt impo

我从两个不同的传感器收集数据,它们以不均匀的间隔异步运行。我想从传感器1获取数据,并将其插入传感器2的时间戳中。 我找到了一种处理熊猫的方法,首先创建一个组合时间序列,对其进行插值,然后将插值后的时间序列与第二个传感器的时间序列相结合,只显示相交时间。 有没有一种更具Pythonic(或Pandaic)的方法来更有效地实现这一点。下面是一个示例代码,它使用了我上面描述的方法:

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'])
使用熊猫,我们可以执行以下操作: 您可以使用from pandas.Index和from pandas.DataFrame,这将消除所有合并:

ax1 = frame1.plot(marker='+')
frame1_r = frame1.reindex(frame1.index.union(frame2.index))\
                 .interpolate(method='index')\
                 .reindex(frame2.index)
frame1_r.plot(ax=ax1, style='o')
输出:


太好了,这就是我要找的。我必须添加一个“drop_duplicates()”以使其按预期工作。谢谢。回答得好@Scott