Ad

How To Interpolate Latitude/longitude And Heading In Pandas

Description: I have a Pandas dataframe formed by three columns: latitude [-90;90], longitude [-180;180] and direction [0;360]. All columns are in degrees. The index is instead formed by date + time like so:

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']))

This is what it looks like:

                     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

GOAL: My goal is to add the missing datetimes between the indexes and perform an interpolation (ex linear) between the missing coordinates and angles. I was able to add the missing dates like so:

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

In order to achieve my goal I tried to use the pandas function pandas.Series.interpolatewithout success because it does not take into account the angle "jumps" between -180;180 for the longitude and the "jump" between 360 and 0 for the direction.

QUESTION: Could you please provide a smart and elengant way to achieve such interpolation so that it takes into account those jumps between the limits of their range?

Note: here there is an example just to be more clear (interpolation between -176 and 176): -176,-177,-178,-179,-180/180,179,178,177,176?

Ad

Answer

Here there is the answer to my question:

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  = df.reindex(idx, fill_value=np.nan)
df.reset_index(drop=False, inplace=True)
df = df.interpolate()#pd.merge(left=pd.DataFrame({'index':idx}), right=df, on='index', how='left').interpolate()

df[['lat','lon','dir']] %= 360
Ad
source: stackoverflow.com
Ad