Forexpython“汇率来源未准备就绪”

2024-07-04 06:50:25 发布

您现在位置:Python中文网/ 问答频道 /正文

我想使用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

以及具有已转换金额的新列:

^{pr2}$

我得到RatesNotAvailableError“汇率来源未准备就绪” 有什么原因吗?它以前只处理少量的数据,但我的实际数据框中有许多行。。。在


Tags: 模块数据代码convertdataframereturn货币结构
1条回答
网友
1楼 · 发布于 2024-07-04 06:50:25

来自源:https://github.com/MicroPyramid/forex-python/blob/80290a2b9150515e15139e1a069f74d220c6b67e/forex_python/converter.py#L73

您的错误意味着库收到了对您的请求的非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://pypi.python.org/pypi/backoff/1.3.1

相关问题 更多 >

    热门问题