奇怪的“货币汇率源未准备好”forex_python 错误

问题描述 投票:0回答:2

我正在尝试通过 python 了解外汇 API。我在下面发布的代码在周五对我有用,并且我收到了所需日期的所有转换率。奇怪的是,当我今天由于某种原因运行代码时,它说

货币汇率源尚未准备好。

为什么会出现这种情况?

from forex_python.converter import CurrencyRates
import pandas as pd
c = CurrencyRates()
from forex_python.converter import CurrencyRates
c = CurrencyRates()


df = pd.DataFrame(pd.date_range(start='8/16/2021 10:00:00', end='8/22/2021 11:00:00', freq='600min'), columns=['DateTime'])

def get_rate(x):
    try:
        op = c.get_rate('CAD', 'USD', x)
    except Exception as re:
        print(re)
        op=None
    return op

df['Rate'] = df['DateTime'].apply(get_rate)

Currency Rates Source Not Ready
Currency Rates Source Not Ready

df
Out[17]: 
              DateTime      Rate
0  2021-08-16 10:00:00  0.796374
1  2021-08-16 20:00:00  0.796374
2  2021-08-17 06:00:00  0.793031
3  2021-08-17 16:00:00  0.793031
4  2021-08-18 02:00:00  0.792469
5  2021-08-18 12:00:00  0.792469
6  2021-08-18 22:00:00  0.792469
7  2021-08-19 08:00:00  0.783967
8  2021-08-19 18:00:00  0.783967
9  2021-08-20 04:00:00  0.774504
10 2021-08-20 14:00:00  0.774504
11 2021-08-21 00:00:00       NaN
12 2021-08-21 10:00:00       NaN
13 2021-08-21 20:00:00       NaN
14 2021-08-22 06:00:00       NaN

如何解决这个问题?有没有办法在拨打电话时忽略

NaN
?我觉得 API 只提供周一到周五上午 10 点到下午 5 点的结果。那么有没有办法获得这些结果。

python pandas currency forex
2个回答
4
投票

python_forex SDK使用The Forex Api,它从欧洲中央银行获取数据。欧洲央行在每个工作日每天更新一次利率(大约在欧洲中部时间 16:00),目标收盘日除外。这意味着您在工作日只能获得一个货币汇率值。

要获得这些费率,您可以这样做

df = pd.DataFrame(pd.date_range(start='8/16/2021 10:00:00', end='8/22/2021 11:00:00', freq='B'), columns=['DateTime'])

其中

freq="B"
表示仅工作日(来源)。运行其余代码时,会导致:

             DateTime      Rate
0 2021-08-16 10:00:00  0.796374
1 2021-08-17 10:00:00  0.793031
2 2021-08-18 10:00:00  0.792469
3 2021-08-19 10:00:00  0.783967
4 2021-08-20 10:00:00  0.774504

您也可以在工作时间使用

freq="BH"
进行操作,但您不会获得不同的费率。

另请注意,在 python_forex SDK 中,他们说您应该使用版本 1.6 以避免 RatesNotAvailableError。

我创建的包

注意!我不打算维护它。

Github链接

安装:

pip install easy-exchange-rates

用途:

from easy_exchange_rates import API
api = API()
time_series = api.get_exchange_rates(
  base_currency="EUR", 
  start_date="2021-01-01", 
  end_date="2021-08-13", 
  targets=["USD","CAD"]
)
data_frame = api.to_dataframe(time_series)
print(data_frame.head(5))
>>>
                 CAD       USD
2021-01-01  1.549988  1.217582
2021-01-02  1.544791  1.213500
2021-01-03  1.557791  1.223409
2021-01-04  1.566076  1.225061
2021-01-05  1.558553  1.229681

0
投票

我还创建了一个包,它返回一个字典,其中包含特定基础货币的特定日期的汇率。对于绘图不太有用,但例如,如果您以不同货币创建发票,则很有用。或者想要更新特定货币的产品价格。

该软件包使用了欧洲央行的数据。请参阅https://github.com/ddofborg/exchange_rates

示例:

>>> from exchange_rates import get_exchange_rates
>>> print( get_exchange_rates('USD', target_currencies=['EUR', 'CAD', 'USD'], on_date='2023-10-01') )
{'EUR': 0.9496676163342831, 'CAD': 1.3613485280151947, 'USD': 1.0}
>>> print( get_exchange_rates('EUR', target_currencies=['EUR', 'CAD', 'USD'], on_date='2023-10-01') )
{'EUR': 1.0, 'CAD': 1.4335, 'USD': 1.053}
© www.soinside.com 2019 - 2024. All rights reserved.