如何用最接近的值对时间戳进行排序?

问题描述

我有 2 个数据框:

  1. 传感器读数
#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()
  1. 温度预测
#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 个。

输出应该是一个带有以下列的数据框-

  1. 传感器时间戳(索引)
  2. 最接近时间戳的温度
  3. 预测值
  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 (将#修改为@)

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...