我想使用Forex-python模块根据特定日期(根据dataframe中的日期,上个月的最后一天)将各种货币的金额转换为特定货币("DKK")。
这是我的代码的结构:
pd.DataFrame(data={'Date':['2017-4-15','2017-6-12','2017-2-25'],'Amount':[5,10,15],'Currency':['USD','SEK','EUR']})
def convert_rates(amount,currency,PstngDate):
PstngDate = datetime.strptime(PstngDate, '%Y-%m-%d')
if currency != 'DKK':
return c.convert(base_cur=currency,dest_cur='DKK',amount=amount \
,date_obj=PstngDate - timedelta(PstngDate.day))
else:
return amount并使用换算后的金额显示新列:
df['Amount, DKK'] = np.vectorize(convert_rates)(
amount=df['Amount'],
currency=df['Currency'],
PstngDate=df['Date']
)我得到了RatesNotAvailableError“货币汇率来源没有准备好”,你知道什么会导致这种情况吗?它以前处理过少量数据,但我在实际的df中有很多行……
发布于 2017-08-22 19:31:14
您的错误意味着库收到了对您的请求的非200响应码。这可能意味着网站关闭了,或者它暂时阻止了你,因为你正在向它发送大量的请求。
尝试将对c.convert的调用替换为以下内容:
from time import sleep
def try_convert(amount, currency, PstngDate):
success = False
while success == False:
try:
res = c.convert(base_cur=currency,dest_cur='DKK',amount=amount \
,date_obj=PstngDate - timedelta(PstngDate.day))
except:
#wait a while
sleep(10)
return res或者更好的是,使用像backoff这样的库来为您进行重试:
https://stackoverflow.com/questions/45816190
复制相似问题