如何在熊猫中插入纬度/经度和航向

时间:2019-06-20 08:56:25

标签: python pandas interpolation latitude-longitude angle

说明:我有一个由三列组成的Pandas数据框:纬度[-90; 90],经度[-180; 180]和方向[0; 360]。所有列均以度为单位。 索引由日期+时间组成,如下所示:

df = pd.DataFrame({'lat':[87,90,85,10,-40,-85,-89,-40],
                   'lon':[-150,-178,176,100,10,1,-20,-100],
                   'dir':[180,200,356,4,20,1,351,20]},
                   index = pd.to_datetime(['2019-06-17 08:29:07','2019-06-17 08:29:11', '2019-06-17 08:29:16', '2019-06-17 08:29:25', '2019-06-17 08:29:33', '2019-06-17 08:29:40', '2019-06-17 08:29:48', '2019-06-17 08:29:57']))

这是它的样子:

                     lat  lon  dir
2019-06-17 08:29:07   87 -150  180
2019-06-17 08:29:11   90 -178  200
2019-06-17 08:29:16   85  176  356
2019-06-17 08:29:25   10  100    4
2019-06-17 08:29:33  -40   10   20
2019-06-17 08:29:40  -85    1    1
2019-06-17 08:29:48  -89  -20  351
2019-06-17 08:29:57  -40 -100   20

目标::我的目标是在索引之间添加缺失的日期时间,并在缺失的坐标和角度之间进行插值(线性)。我能够像这样添加缺少的日期:

idx = pd.to_datetime(pd.date_range(df.index[0], df.index[-1], freq='s').strftime('%Y-%m-%d %H:%M:%S'))
df  = df.reindex(idx, fill_value='NaN')

                     lat   lon  dir
2019-06-17 08:29:07   87  -150  180
2019-06-17 08:29:08  NaN   NaN  NaN
2019-06-17 08:29:09  NaN   NaN  NaN
2019-06-17 08:29:10  NaN   NaN  NaN
2019-06-17 08:29:11   90  -178  200
2019-06-17 08:29:12  NaN   NaN  NaN
2019-06-17 08:29:13  NaN   NaN  NaN
...................  ...   ...  ...
2019-06-17 08:29:55  NaN   NaN  NaN
2019-06-17 08:29:56  NaN   NaN  NaN
2019-06-17 08:29:57  -40  -100   20

为了实现我的目标,我尝试不成功使用pandas函数pandas.Series.interpolate,因为它没有考虑到-180; 180之间的角度“跳跃”和180之间的“跳跃”角度方向为0。

问题::能否请您提供一种巧妙而精巧的方法来实现这种插值,以便考虑到其范围限制之间的跳跃?

注意:这里有一个例子只是为了更清楚(-176和176之间的插值):-176,-177,-178,-179,-180 / 180,179,178,177,176?

1 个答案:

答案 0 :(得分:0)

这是我的问题的答案:

df.reset_index(drop=False, inplace=True)
df['dir'] = np.rad2deg(np.unwrap(np.deg2rad(df['dir'])))
df['lat'] = np.rad2deg(np.unwrap(np.deg2rad(df['lat'])))
df['lon'] = np.rad2deg(np.unwrap(np.deg2rad(df['lon'])))
df = pd.merge(left=idx, right=df, on='index', how='left').interpolate()
df[['lat','lon','dir']] %= 360
print(df)