奇怪的'Currency Rates source not ready'forex_python错误

Strange 'Currency Rates source not ready' forex_python error

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

Currency Rates source not ready.

为什么会这样?

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_forex SDK uses The Forex Api, which gets its data from The European Central Bank. The European Central Bank updates the rates on a daily interval (at approximately 16:00 CET) on every working day, except on TARGET closing days。这意味着您在工作日只能获得一个货币汇率值。

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

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" 仅表示工作日 (source)。当 运行 其余代码将导致:

             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 link

安装:

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