I have a Pandas
dataframe with ~20k rows, and I am trying to geocode by address column into lat/long coordinates.
How do I use time.sleep()
or maybe other function to stop OSM Nominatim from Too Many Requests 429 error
that I am getting now?
Here's the code I use for this:
from geopy.geocoders import Nominatim
from geopy.distance import vincenty
geolocator = Nominatim()
df['coord'] = df['address'].apply(geolocator.geocode).apply(lambda x: (x.latitude, x.longitude))
df.head()
Thanks in advance!
geopy since 1.16.0 includes a
RateLimiter
class which provides a convenient way to deal with theToo Many Requests 429 error
by adding delays between the queries and retrying the failed requests.Docs: https://geopy.readthedocs.io/en/1.16.0/#usage-with-pandas
I would imagine you use a for loop. Without seeing your data, it would look something like this.
This is how you would implement sleep to wait 2 seconds before running the loop again. Also, in the event that the geolocator cannot find the latitude and longitude, it will pass instead of exiting out of the loop and having you start over.