- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我有一个带有地址列的大型 DataFrame:
data addr
0 0.617964 IN,Krishnagiri,635115
1 0.635428 IN,Chennai,600005
2 0.630125 IN,Karnal,132001
3 0.981282 IN,Jaipur,302021
4 0.715813 IN,Chennai,600005
...
并且我编写了以下函数来将地址替换为地址的经纬度坐标:
from geopy.geocoders import Nominatim
geo_locator = Nominatim(user_agent="MY_APP_ID")
def get_coordinates(addr):
location = geo_locator.geocode(addr)
if location is not None:
return pd.Series({'lat': location.latitude, 'lon': location.longitude})
location = geo_locator.geocode(addr.split(',')[0])
if location is not None:
return pd.Series({'lat': location.latitude, 'lon': location.longitude})
return pd.Series({'lat': -1, 'lon': -1})
然后在地址列上调用 pandas apply 方法,并将结果连接到 DF 的末尾而不是地址列:
df = pd.concat([df, df.addr.apply(get_coordinates)], axis=1).drop(['addr'], axis=1)
但是,由于 get_coordinates 调用第 3 方 API,它失败了:geopy.exc.GeocoderTimedOut:服务超时
如何限制请求以确保在继续下一个值之前得到响应?
更新:
为了进一步改进,我想仅针对唯一值调用 API,即:如果地址 IN,Krishnagiri,635115
在我的 DataFrame 中出现 20 次,我只想调用它一次并应用所有 20 次出现的结果。
更新 2:
日志 + 堆栈跟踪,@Andrew Lavers 代码:
...
Fetched Gandipet, Khanapur, Rangareddy District, Telangana, 500075, India
Fetched Jaipur Municipal Corporation, Jaipur, Rajasthan, 302015, India
Fetched Chennai, Chennai district, Tamil Nadu, India
Exception from geolocator: Fake exception for testing
Backing off for 1 seconds.
Exception from geolocator: Fake exception for testing
Backing off for 3 seconds.
Fetched None
Traceback (most recent call last):
File "/usr/local/Cellar/python3/3.6.1/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/geopy/geocoders/base.py", line 344, in _call_geocoder
page = requester(req, timeout=timeout, **kwargs)
File "/usr/local/Cellar/python3/3.6.1/Frameworks/Python.framework/Versions/3.6/lib/python3.6/urllib/request.py", line 526, in open
response = self._open(req, data)
File "/usr/local/Cellar/python3/3.6.1/Frameworks/Python.framework/Versions/3.6/lib/python3.6/urllib/request.py", line 544, in _open
'_open', req)
File "/usr/local/Cellar/python3/3.6.1/Frameworks/Python.framework/Versions/3.6/lib/python3.6/urllib/request.py", line 504, in _call_chain
result = func(*args)
File "/usr/local/Cellar/python3/3.6.1/Frameworks/Python.framework/Versions/3.6/lib/python3.6/urllib/request.py", line 1361, in https_open
context=self._context, check_hostname=self._check_hostname)
File "/usr/local/Cellar/python3/3.6.1/Frameworks/Python.framework/Versions/3.6/lib/python3.6/urllib/request.py", line 1321, in do_open
r = h.getresponse()
File "/usr/local/Cellar/python3/3.6.1/Frameworks/Python.framework/Versions/3.6/lib/python3.6/http/client.py", line 1331, in getresponse
response.begin()
File "/usr/local/Cellar/python3/3.6.1/Frameworks/Python.framework/Versions/3.6/lib/python3.6/http/client.py", line 297, in begin
version, status, reason = self._read_status()
File "/usr/local/Cellar/python3/3.6.1/Frameworks/Python.framework/Versions/3.6/lib/python3.6/http/client.py", line 258, in _read_status
line = str(self.fp.readline(_MAXLINE + 1), "iso-8859-1")
File "/usr/local/Cellar/python3/3.6.1/Frameworks/Python.framework/Versions/3.6/lib/python3.6/socket.py", line 586, in readinto
return self._sock.recv_into(b)
File "/usr/local/Cellar/python3/3.6.1/Frameworks/Python.framework/Versions/3.6/lib/python3.6/ssl.py", line 1002, in recv_into
return self.read(nbytes, buffer)
File "/usr/local/Cellar/python3/3.6.1/Frameworks/Python.framework/Versions/3.6/lib/python3.6/ssl.py", line 865, in read
return self._sslobj.read(len, buffer)
File "/usr/local/Cellar/python3/3.6.1/Frameworks/Python.framework/Versions/3.6/lib/python3.6/ssl.py", line 625, in read
v = self._sslobj.read(len, buffer)
socket.timeout: The read operation timed out
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/Users/...//tmp.py", line 89, in <module>
df.addr.apply(get_coordinates)
File "/usr/local/Cellar/python3/3.6.1/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/pandas/core/series.py", line 3194, in apply
mapped = lib.map_infer(values, f, convert=convert_dtype)
File "pandas/_libs/src/inference.pyx", line 1472, in pandas._libs.lib.map_infer
File "/Users/...//tmp.py", line 76, in get_coordinates
location = geo_locator.geocode(addr.split(',')[0])
File "/usr/local/Cellar/python3/3.6.1/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/geopy/geocoders/osm.py", line 307, in geocode
self._call_geocoder(url, timeout=timeout), exactly_one
File "/usr/local/Cellar/python3/3.6.1/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/geopy/geocoders/base.py", line 371, in _call_geocoder
raise GeocoderTimedOut('Service timed out')
geopy.exc.GeocoderTimedOut: Service timed out
Process finished with exit code 1
最佳答案
这里有一些经过测试的代码可能会有所帮助。 1) 对 Api 指定的简单速率限制(Nominatum 似乎是每秒 1 个,但我成功地低至 0.1 秒)。 2) 字典中的简单结果缓存,可通过测试参数控制 3) 具有乘法减速和线性加速的重试循环。 (减速快,加速慢)4)伪造错误的测试异常
我无法复制您遇到的问题 - 可能是因为您的 API 路径。
一个更健壮的策略可能是构建本地持久缓存并继续重试,直到构建完整的批处理。缓存可以是作为 csv 写入文件的 pandas 数据帧。整体伪代码是这样的。
repeat until all addresses are in the cache
cache = pd.read_csv("cache.csv)
addressess_to_get = addresses in df that are not in cache
for batch of n addresses in addresses_to_get:
cache.add(get_location(addr))
cache.write_csv("cache.csv")
这是测试代码
import datetime
import time
import pandas as pd
from geopy.geocoders import Nominatim
geo_locator = Nominatim(user_agent="notarealemail@gmail.com")
# Define the rate limit function and associated global variable
last_time = datetime.datetime.now()
backoff_time = 0
def rate_limit(min_interval_seconds = .1):
global last_time
sleep = min_interval_seconds - (datetime.datetime.now() - last_time).total_seconds()
if sleep > 0 :
print(f'Sleeping for {sleep} seconds')
time.sleep(sleep)
last_time = datetime.datetime.now()
# make a cache dictionary keyed by address
geo_cache = {}
backoff_seconds = 0
def get_coordinates_with_retry(addr):
# Return coords from global cache if it exists
global backoff_seconds
# set the backoff intital values and factors
max_backoff_seconds = 60
backoff_exponential = 2
backoff_linear = 2
# rate limited API call
rate_limit()
# Retry until max_back_seconds is reached
while backoff_seconds < max_backoff_seconds: # backoff up to this time
if backoff_seconds > 0:
print(f"Backing off for {backoff_seconds} seconds.")
time.sleep(backoff_seconds)
try:
location = geo_locator.geocode(addr)
# REMOVE THIS: fake an error for testing
#import random
#if random.random() < .3:
# raise(Exception("Fake exception for testing"))
# Success - so reduce the backoff linearly
print (f"Fetched {location} for address {addr}")
backoff_seconds = backoff_seconds - backoff_linear if backoff_seconds > backoff_linear else 0
break
except Exception as e:
print(f"Exception from geolocator: {e}")
# Backoff exponentially
backoff_seconds = 1 + backoff_seconds * backoff_exponential
if backoff_seconds > max_backoff_seconds:
raise Exception("Max backoff reached\n")
return(location)
def get_coordinates(addr, useCache = True):
# Return from cache if previously loaded
global geo_cache
if addr in geo_cache:
return geo_cache[addr]
# Attempt using the full address
location = get_coordinates_with_retry(addr)
# Attempt using the first part only if None found
if location is not None:
result = pd.Series({'lat': location.latitude, 'lon': location.longitude})
else :
print (f"Trying split address for address {addr}")
location = get_coordinates_with_retry(addr.split(',')[0])
if location is not None:
result = pd.Series({'lat': location.latitude, 'lon': location.longitude})
else:
result = pd.Series({'lat': -1, 'lon': -1})
# assign to cache
if useCache:
geo_cache[addr] = result
return(result)
# Use the test data
df = pd.DataFrame({'addr' : [
'IN,Krishnagiri,635115',
'IN,Chennai,600005',
'IN,Karnal,132001',
'IN,Jaipur,302021',
'IN,Chennai,600005']})
# repeat the test data to make alarger set
df = pd.concat([df, df, df, df, df, df, df, df, df, df])
df.addr.apply(get_coordinates)
print(f"Address cache contains {len(geo_cache)} address locations.")
关于python - 使用 API 调用时, throttle Pandas 适用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53466252/
我正在处理一组标记为 160 个组的 173k 点。我想通过合并最接近的(到 9 或 10 个组)来减少组/集群的数量。我搜索过 sklearn 或类似的库,但没有成功。 我猜它只是通过 knn 聚类
我有一个扁平数字列表,这些数字逻辑上以 3 为一组,其中每个三元组是 (number, __ignored, flag[0 or 1]),例如: [7,56,1, 8,0,0, 2,0,0, 6,1,
我正在使用 pipenv 来管理我的包。我想编写一个 python 脚本来调用另一个使用不同虚拟环境(VE)的 python 脚本。 如何运行使用 VE1 的 python 脚本 1 并调用另一个 p
假设我有一个文件 script.py 位于 path = "foo/bar/script.py"。我正在寻找一种在 Python 中通过函数 execute_script() 从我的主要 Python
这听起来像是谜语或笑话,但实际上我还没有找到这个问题的答案。 问题到底是什么? 我想运行 2 个脚本。在第一个脚本中,我调用另一个脚本,但我希望它们继续并行,而不是在两个单独的线程中。主要是我不希望第
我有一个带有 python 2.5.5 的软件。我想发送一个命令,该命令将在 python 2.7.5 中启动一个脚本,然后继续执行该脚本。 我试过用 #!python2.7.5 和http://re
我在 python 命令行(使用 python 2.7)中,并尝试运行 Python 脚本。我的操作系统是 Windows 7。我已将我的目录设置为包含我所有脚本的文件夹,使用: os.chdir("
剧透:部分解决(见最后)。 以下是使用 Python 嵌入的代码示例: #include int main(int argc, char** argv) { Py_SetPythonHome
假设我有以下列表,对应于及时的股票价格: prices = [1, 3, 7, 10, 9, 8, 5, 3, 6, 8, 12, 9, 6, 10, 13, 8, 4, 11] 我想确定以下总体上最
所以我试图在选择某个单选按钮时更改此框架的背景。 我的框架位于一个类中,并且单选按钮的功能位于该类之外。 (这样我就可以在所有其他框架上调用它们。) 问题是每当我选择单选按钮时都会出现以下错误: co
我正在尝试将字符串与 python 中的正则表达式进行比较,如下所示, #!/usr/bin/env python3 import re str1 = "Expecting property name
考虑以下原型(prototype) Boost.Python 模块,该模块从单独的 C++ 头文件中引入类“D”。 /* file: a/b.cpp */ BOOST_PYTHON_MODULE(c)
如何编写一个程序来“识别函数调用的行号?” python 检查模块提供了定位行号的选项,但是, def di(): return inspect.currentframe().f_back.f_l
我已经使用 macports 安装了 Python 2.7,并且由于我的 $PATH 变量,这就是我输入 $ python 时得到的变量。然而,virtualenv 默认使用 Python 2.6,除
我只想问如何加快 python 上的 re.search 速度。 我有一个很长的字符串行,长度为 176861(即带有一些符号的字母数字字符),我使用此函数测试了该行以进行研究: def getExe
list1= [u'%app%%General%%Council%', u'%people%', u'%people%%Regional%%Council%%Mandate%', u'%ppp%%Ge
这个问题在这里已经有了答案: Is it Pythonic to use list comprehensions for just side effects? (7 个答案) 关闭 4 个月前。 告
我想用 Python 将两个列表组合成一个列表,方法如下: a = [1,1,1,2,2,2,3,3,3,3] b= ["Sun", "is", "bright", "June","and" ,"Ju
我正在运行带有最新 Boost 发行版 (1.55.0) 的 Mac OS X 10.8.4 (Darwin 12.4.0)。我正在按照说明 here构建包含在我的发行版中的教程 Boost-Pyth
学习 Python,我正在尝试制作一个没有任何第 3 方库的网络抓取工具,这样过程对我来说并没有简化,而且我知道我在做什么。我浏览了一些在线资源,但所有这些都让我对某些事情感到困惑。 html 看起来
我是一名优秀的程序员,十分优秀!