I am trying to execute the following function that was published under the same title in StackOverflow. But I get this error that "loop of ufunc does not support argument 0 of type Series which has no callable radians method." I have checked the dtype() of lat and lng in my dataframe and they are both float64 and of the same length. Can you please help me to figure out what I am doing wrong here?
Thanks, MA
def haversine(lat1, lon1, lat2, lon2, to_radians=True, earth_radius=6371):
if to_radians:
lat1, lon1, lat2, lon2 = np.radians([lat1, lon1, lat2, lon2])
a = np.sin((lat2-lat1)/2.0)**2 + \
np.cos(lat1) * np.cos(lat2) * np.sin((lon2-lon1)/2.0)**2
return earth_radius * 2 * np.arcsin(np.sqrt(a))
df['dist'] = \
haversine(df.lat.shift(), df.lng.shift(),
df.loc[1:, 'lat'], df.loc[1:, 'lng'])
Estimating the difference in distance after sorting the dataframe.