I got a dataframe with two columns that are holding Longitude and Latitude coordinates:
import pandas as pd
values = {\'Latitude\': {0: 47.0215033656
Since pandas v0.23 it is now possible to pass a Series instead of a ndarray to Rolling.apply(). Just set raw=False
.
raw : bool, default None
False
: passes each row or column as a Series to the function.
True
orNone
: the passed function will receive ndarray objects instead. If you are just applying a NumPy reduction function this will achieve much better performance. The raw parameter is required and will show a FutureWarning if not passed. In the future raw will default to False.New in version 0.23.0.
So building on your given example, you could move the latitude to the index and pass the whole longitude series---including the index---to your function:
df = df.set_index('Latitude')
df['Distance'] = df['Longitude'].rolling(2).apply(haversine_distance, raw=False)