I am trying to calculate the distance (in km) between different geolocations with latitude and longitude. I tried to use the code from this thread: Pandas Latitude-Longitude to distance between successive rows. However, I run into this error:
Does anyone know how to fix this issue?
---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
~\Anaconda3\lib\site-packages\pandas\core\generic.py in __getattr__(self, name)
5464 return self[name]
-> 5465 return object.__getattribute__(self, name)
5466
AttributeError: 'Series' object has no attribute 'radians'
The above exception was the direct cause of the following exception:
TypeError Traceback (most recent call last)
<ipython-input-56-3c590360590e> in <module>
11
12 df['dist'] = haversine(df.latitude.shift(), df.longitude.shift(),
---> 13 df.loc[1:, 'latitude'], df.loc[1:, 'longitude'])
14
15
<ipython-input-56-3c590360590e> in haversine(lat1, lon1, lat2, lon2, to_radians, earth_radius)
2 def haversine(lat1, lon1, lat2, lon2, to_radians=True, earth_radius=6371):
3 if to_radians:
----> 4 lat1, lon1, lat2, lon2 = np.radians([lat1, lon1, lat2, lon2])
5
6 a = np.sin((lat2-lat1)/2.0)**2 + \
TypeError: loop of ufunc does not support argument 0 of type Series which has no callable radians method
Here is the data frame:
>>> df_latlon
latitude longitude
0 37.405548 -122.078481
1 34.080610 -84.200785
2 37.770830 -122.395463
3 37.773792 -122.409865
4 41.441269 -96.494304
5 41.441269 -96.494304
6 41.441269 -96.494304
7 41.883784 -87.637668
8 26.140780 -80.124434
9 39.960000 -85.983660
Here is the code:
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_latlon['dist'] = haversine(df_latlon.latitude.shift(), df_latlon.longitude.shift(),
df_latlon.loc[1:, 'latitude'], df_latlon.loc[1:, 'longitude'])