有效地为熊猫数据框中的每一行找到最接近的时间戳

时间:2019-10-10 19:22:39

标签: python pandas

上下文:     我有两个以不同频率(唯一时间戳)记录的信号。我想确定信号二等于零时信号一的所有值。本质上,我想确定车辆不运动时IMU的直流偏置。

我想找出一种在信号二接近零时隔离信号一值的更有效方法。

我的熊猫数据帧包含CAN数据

time, message, signal, value
1.05, Veh, Speed, 1
1.1, IMU, Yaw_Rate, 1.01
1.2, IMU, Yaw_Rate, 1.2
2.0, Veh, Speed, 0
2.4, IMU, Yaw_Rate, 1.08
2.6, IMU, Yaw_Rate, 1.24
3.01, Veh, Speed, 0
3.2, IMU, Yaw_Rate, 1.08
3.9, IMU, Yaw_Rate, 1.24
4.0, Veh, Speed, 1

用“ Vehicle_Speed_KPH_Integer”替换“ Speed”

import pandas as pd
import numpy as np

def bias_filter_index_identifier(df, signal_name):
    ''' This is a highly inneficient function to find all the values of a signal while the
    vehicle is stopped.
    Creating masks (windows) may be more efficient'''
    alpha = 0.001
    y = []
    y_filt = []
    Veh_Speed = df.loc[(df['signal'] == 'Vehicle_Speed_KPH_Integer')]
    Veh_Speed = fix_df_float(Veh_Speed)

    signal = df.loc[(df['signal'] == signal_name)]
    signal = fix_df_float(signal)
    for i in range(0, len(signal)):
        try:
            time = signal.iloc[i]['time']
            value = signal.iloc[i]['value']
            # Find the index of the closest point in vehicle speed
            idx = Veh_Speed['time'].subtract(time).abs().idxmin()
            #df.loc[Yaw_Rate['value'].idxmax(axis=0)]
            Single_Speed_Data = df.loc[idx]
            if Single_Speed_Data['value'] == 0:
                y.append([time, value])
                if len(y) == 1:
                    filt_value = value
                else:
                    filt_value = alpha*value + (1 - alpha) * np.asarray(y_filt[-1][1])
                y_filt.append([time, filt_value])
                #print(i)
        except Exception as err:
            print("We had an error on item {}".format(i))
            print(err)

    y1 = np.array(y)
    y2 = np.array(y_filt)

    df_filt = pd.DataFrame(data=y2, columns=['time', 'value'])
    df_filt['message'] = "custom"
    df_filt['signal'] = signal_name + "_Filtered"
    df_filt.drop_duplicates(subset='time', keep='first', inplace=True)

    return df_filt

0 个答案:

没有答案