如何解决如何用最接近的值对时间戳进行排序?
我有 2 个数据框:
- 传感器读数
#Making a dataset with sensor readings
index = pd.date_range(start='1/12/2020',end='4/12/2020',freq='0.5H')
columns = ['Sensor ' + str(n) for n in range(1,5)]
data = np.random.uniform(1,25,size=(4369,4))
df_sensor_readings = pd.DataFrame(data,index=index,columns=columns).head()
- 温度预测
#Making a dataset with temperature predictions
index = pd.date_range(start='1/12/2020',end='6/12/2020',freq='0.7H')
columns = ['Sensor ' + str(n) for n in range(1,size=(5212,4))
df_temp_predictions = pd.DataFrame(data,columns=columns)
#Taking 10% null values in the dataset
for col in df_temp_predictions.columns:
df_temp_predictions.loc[df_temp_predictions.sample(frac=0.1).index,col] = pd.np.nan
df_temp_predictions.head()
目标是将传感器读数与最接近的温度预测相匹配。最接近我的意思是最接近的时间戳。可能有 100 个传感器,但最初我只使用了 4 个。
输出应该是一个带有以下列的数据框-
- 传感器时间戳(索引)
- 最接近时间戳的温度
- 预测值
- 传感器类型(例如 Sensor1 或 Sensor2 等)
到目前为止我已经达到-
def nearest(ts,s):
# Given a presorted list of timestamps: s = sorted(index)
i = bisect.bisect_left(s,ts)
return min(s[max(0,i-1): i+2],key=lambda t: abs(ts - t))
val_sensor_readings = df_sensor_readings.index.values
predictions_ts= df_temp_predictions.index.values
for temp_ts in val_sensor_readings:
closest_timestamp = nearest(temp_ts,predictions_ts)
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。