货币汇率来源未准备就绪

货币汇率来源未准备就绪

本文介绍了Forex-python“货币汇率来源未准备就绪";的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用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(金额,货币,PstngDate):PstngDate = datetime.strptime(PstngDate,'%Y-%m-%d')如果货币!='DKK':返回c.convert(base_cur = currency,dest_cur ='DKK',amount = amount \,date_obj = PstngDate-timedelta(PstngDate.day))别的:退货金额 

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

  df ['Amount,DKK'] = np.vectorize(convert_rates)(amount = df ['Amount'],currency = df ['Currency'],PstngDate = df ['Date']) 

我收到RatesNotAvailableError货币汇率源未准备好"知道是什么原因造成的吗?它以前只能处理少量数据,但是我的真实df中有很多行...

解决方案

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

您的错误表示图书馆收到了您请求的非200响应代码.这可能意味着该站点已关闭,或者由于您正在处理请求而暂时阻止了该站点.

尝试使用以下方式替换对 c.convert 的调用:

 从时间导入睡眠def try_convert(金额,货币,PstngDate):成功=错误成功==错误时:尝试:res = c.convert(base_cur = currency,dest_cur ='DKK',amount = amount \,date_obj = PstngDate-timedelta(PstngDate.day))除了:#稍等片刻睡眠(10)返回资源 

或更妙的是,使用backoff之类的库来为您进行重试:

https://pypi.python.org/pypi/backoff/1.3.1

I want to use the Forex-python module to convert amounts in various currencies to a specific currency ("DKK") according to a specific date (The last day of a previous month according to a date in the dataframe)

This is the structure of my code:

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

and the the new column with the converted amount:

df['Amount, DKK'] = np.vectorize(convert_rates)(
    amount=df['Amount'],
    currency=df['Currency'],
    PstngDate=df['Date']
)

I get the RatesNotAvailableError "Currency Rates Source Not Ready"Any idea what can cause this? It has previously worked with small amounts of data, but I have many rows in my real df...

解决方案

From the source: https://github.com/MicroPyramid/forex-python/blob/80290a2b9150515e15139e1a069f74d220c6b67e/forex_python/converter.py#L73

Your error means the library received a non 200 response code to your request. This could mean the site is down, or it's blocked you momentarily because you're hammering it with requests.

Try replacing the call to c.convert with something like:

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

Or even better, use a library like backoff, to do the retrying for you:

https://pypi.python.org/pypi/backoff/1.3.1

这篇关于Forex-python“货币汇率来源未准备就绪";的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-24 10:11