3

I have the following data frame in a Jupyter Notebook that has a list of GPS coordinates with from geopy.geocoders import Nominatim and import pandas as pd.

    stop_id     Lat         Long
0   2        53.352280  -6.263668
1   3        53.352345  -6.263758
2   4        53.352604  -6.264143
3   6        53.352783  -6.264417
4   7        53.352867  -6.264543
5   8        53.353287  -6.265152

I have been trying to add a new column populated with the corresponding addresses to the GPS coordinates.

To do this I tried

df['address'] = geolocator.reverse((df['Lat'], df['Long']))

but got the following error message:

ValueError: Must be a coordinate pair or Point.

I then created another column [LatLong]

df['LatLong'] = df[df.columns[1:]].apply(
    lambda x: ', '.join(x.dropna().astype(float).astype(str)),axis=1)

    stop_id     Lat         Long         LatLong
0   2       53.352280   -6.263668    53.35228, -6.263668
1   3       53.352345   -6.263758    53.352345, -6.263758
2   4       53.352604   -6.264143    53.352604, -6.264143
3   6       53.352783   -6.264417    53.352783, -6.264417
4   7       53.352867   -6.264543    53.352867, -6.264543
5   8       53.353287   -6.265152    53.353287, -6.265152

I then ran the the following code:

df['address'] = geolocator.reverse(df['LatLong'])

however, I just get the exact same error message.

The code I have used above is adapted from other answers on this site to similar questions and GeoPy's documentation, so I am presuming my code is not exact enough to extract the GPS coordinates in the correct way for geopy.

Can anyone point out my error to me?

Stephen Rauch
  • 47,830
  • 31
  • 106
  • 135
DreamingMan
  • 73
  • 2
  • 7

2 Answers2

1

Problem

Your error message says:

ValueError: Must be a coordinate pair or Point

In both:

df['address'] = geolocator.reverse((df['Lat'], df['Long']))

and

df['address'] = geolocator.reverse(df['LatLong'])

you are sending a pandas structure into a method that does not understand them.

Solution

I have no way to test this, but a solution can look something like:

df['address'] = df.apply(
    lambda row: geolocator.reverse((row['Lat'], row['Long'])), axis=1)
Community
  • 1
  • 1
Stephen Rauch
  • 47,830
  • 31
  • 106
  • 135
  • Hi, thanks for the help! I tried your solution, but unfortunately I got the error message: GeocoderTimedOut: ('Service timed out', 'occurred at index 231'). I would be happy to give further information if you you think it would help. – DreamingMan Jul 30 '17 at 15:40
  • That would be an entirely different problem.... And thus a different question. And sadly a topic I am not familiar with. – Stephen Rauch Jul 30 '17 at 15:44
  • Ok, no problem. I appreciate you taking the time to help. – DreamingMan Jul 30 '17 at 15:51
0

"A large number of DataFrame rows might produce a significant amount of geocoding requests to a Geocoding service, which might be throttled by the service (e.g. by returning Too Many Requests 429 HTTP error or timing out).

geopy.extra.rate_limiter.RateLimiter class provides a convenient wrapper, which can be used to automatically add delays between geocoding calls to reduce the load on the Geocoding service. Also it can retry failed requests and swallow errors for individual rows."

I found this on Geopy Documentation. Maybe you should change tre RateLimiter , see if it helps

julia
  • 1