Python 角度插值

Python 角度插值,python,Python,我试图插入列表中的角度 Dir DirOffset 0 109.6085 30 77.5099 60 30.5287 90 -10.2748 120 -75.359 150 -147.6015 180 -162.7055 210 21.0103 240 3.5502 270 -11.5475 300 -39.8371 330 -109.5473 360 109.6085 我已经写了插值角度的代码,它一直在计算中间角度的平均值,以达到插值值,这需要很长时间。请帮助我,如果有人有更快和更

我试图插入列表中的角度

Dir DirOffset
0   109.6085
30  77.5099
60  30.5287
90  -10.2748
120 -75.359
150 -147.6015
180 -162.7055
210 21.0103
240 3.5502
270 -11.5475
300 -39.8371
330 -109.5473
360 109.6085
我已经写了插值角度的代码,它一直在计算中间角度的平均值,以达到插值值,这需要很长时间。请帮助我,如果有人有更快和更短的代码


我在stackoverflow搜索答案后得到了上述问题的解决方案,然后根据我的要求修改了一点以得到解决方案。这个解决方案可能对需要它的人有用

我使用下面的函数为每个方向箱0,30,60…360到360360插入了度数,并且0度将是相同的,并将它们存储在字典中,以创建一个DataFramepandas DataFrame,并将其与主DataFrame左连接并进一步处理

def插值格式109.6085,77.5099

将以0.1 0.0,0.1,0.2,0.3…28.7,29.8,29.9的间隔返回方向偏移0到30度的插值数组

import numpy as np
from math import fabs

def InterpolateDegrees(start, end, BinSector=12):
    BinAngle = 360/BinSector
    amount = np.arange(0,1,(1/(BinAngle/0.1)))
    dif = fabs(end-start)
    if dif >180:
        if end>start:
            start+=360
        else:
            end+=360

    #Interpolate it
    value = (start + ((end-start)*amount))

    #Wrap it
    rzero = 360

    Arr = np.where((value>=0) & (value<=360), (value), (value % rzero))
    return Arr

这里是一个基于Pandas/Numpy的解决方案,用于使用NaN数据插值角度序列

import pandas as pd
import numpy as np

def interpolate_degrees(series: pd.Series) -> pd.Series:
    # I don't want to modify in place 
    series = series.copy()
    
    # convert to radians
    a = np.radians(series)
    
    # unwrap if not nan
    a[~np.isnan(a)] = np.unwrap(a[~np.isnan(a)])
    series.update(a)
    
    # interpolate unwrapped values
    interpolated = series.interpolate()
    
    # wrap 0 - 360 (2*pi)
    wrapped = (interpolated + 2*np.pi) % (2 * np.pi)
    
    # cconvert back to degrees
    degrees = np.degrees(wrapped)
    series.update(degrees)
    return series
用法:

angle = [350, np.nan, 355, np.nan, 359, np.nan, 1, np.nan, 10]
df = pd.DataFrame(data=angle, columns=['angle'])

df['interpolated'] = interpolate_degrees(df.angle)
angle = [350, np.nan, 355, np.nan, 359, np.nan, 1, np.nan, 10]
df = pd.DataFrame(data=angle, columns=['angle'])

df['interpolated'] = interpolate_degrees(df.angle)