如何以秒值插值每小时的数据?

2024-06-06 13:29:20 发布

您现在位置:Python中文网/ 问答频道 /正文

我有以下pandas dataframedf

    L_Time                U_Time                Eval_Time         L_Flux U_Flux
    2018-05-01 04:30:00   2018-05-01 05:30:00   2018-05-01 05:23:45   100   200
    2018-05-01 07:30:00   2018-05-01 08:30:00   2018-05-01 07:44:11   100   200    

LU-Flux分别包含熊猫时间戳LU-Time的辐射通量值。我想用内插法计算出Eval\u Time时的通量值,以秒为单位。我怎样才能正确地使用Python或熊猫呢。我试着用pandas和scipy线性插值,但总是得到中间值(150)。我想在第二个时间戳(Eval\u Time)根据它与两个小时时间戳的距离插值通量。在


Tags: 距离pandastimeeval时间单位scipy插值
2条回答

我需要重新采样L\U TimeU Time之间的数据(以秒为单位)(上采样),然后插值上采样的通量值(以前是NaN,因为它们丢失了),然后在Eval斨Time提取插值通量值。在

INTERPOL_FLUX = []
for i in df.itertuples():
    df = pd.DataFrame( [(i[1],i[4]), (i[2],i[5])], columns = ['Times', 'Flux'] ) #Create a new dataframe with two Timestamps in a single row
    df = df.set_index('Times') #Set Timestamps as index of new dataframe
    df = pd.Series(df['Flux'], index = df.index)  #Squeeze dataframe to series
    interpolated  = df.resample('S').interpolate(method='linear') #Upsample data and interpolate (i needed linear ones)
    interpol_flux = interpolated.loc[ i[3] ] #Extract interpolated flux at Eval_Time
    INTERPOL_FLUX.append(interpol_flux) #Add this interpolated flux to an empty list

df['Eval_Flux'] = INTERPOL_FLUX  #Set this list as the Eval_Flux column

言简意赅

^{pr2}$

我以为会很慢,但很快。在

你可以做你自己的插值,因为它是在两列之间。不过,您的数据似乎不正确,因为您要求在第二行进行推断。不管怎样,下面会给你一个答案

df = pd.DataFrame(data={'L_Time':['2018-05-01 04:30:00','2018-05-03 07:30:00'],
    'U_Time':['2018-05-01 05:30:00','2018-05-01 08:30:00'],
    'Eval_Time':['2018-05-01 05:23:45','2018-05-01 07:44:11'],
    'L_Flux':[ 100 ,100],
    'U_Flux':[200,200]})

df['L_Time'] = pd.to_datetime(df['L_Time'])
df['U_Time'] =  pd.to_datetime(df['U_Time'])
df['Eval_Time'] =  pd.to_datetime(df['Eval_Time'])

# The actual maths part - using times between U, L and Eval
df['Eval_Flux'] = df.L_Flux + (df.U_Flux - df.L_Flux)*(df.Eval_Time - df.L_Time)/(df.U_Time - df.L_Time)



               L_Time              U_Time          Eval_Time  L_Flux  U_Flux Eval_Flux
0 2018-05-01 04:30:00 2018-05-01 05:30:00 2018-05-01 05:23:45     100     200     189.583333   
1 2018-05-03 07:30:00 2018-05-01 08:30:00 2018-05-01 07:44:11     100     200     201.624704

相关问题 更多 >