【问题标题】:Forex-python "Currency Rates Source Not Ready"Forex-python“货币汇率来源未准备好”
【发布时间】:2017-08-22 11:18:56
【问题描述】:

我想使用 Forex-python 模块根据特定日期将各种货币的金额转换为特定货币(“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 "Currency Rates Source Not Ready" 知道什么会导致这种情况吗?它以前处理过少量数据,但我的真实 df 中有很多行...

【问题讨论】:

  • c.convert() 调用什么库?如果它正在调用一些外部源,那么您可能会受到速率限制,因为np.vectorize 调用过于频繁。检查文档以获取 c 的任何内容,然后您可能需要按顺序进行查找。
  • 啊,抱歉:它正在调用 forex-python 库 - 你知道我该如何绕过这个限制吗?
  • github.com/MicroPyramid/forex-python/blob/… 看起来图书馆没有谈论配额。您的错误只是表明源返回了非 200 响应代码。建议改为使用重试功能和某种退避来连续进行计算

标签: python pandas currency


【解决方案1】:

来源: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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-04-27
    • 2020-12-24
    • 2021-01-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-12-30
    相关资源
    最近更新 更多