data.get_data_yahoo在某些股票代号上引发错误

时间:2019-06-09 11:42:17

标签: python yahoo-finance pandas-datareader

运行pandas data_reader代码会在某些股票代码上引发错误

运行以下代码:

import pandas as pd
import pandas_datareader as dr
%matplotlib inline
df = dr.data.get_data_yahoo('FRE.DE',start='2018-10-1', end='2018-11-30')

引发以下错误:

---------------------------------------------------------------------------
KeyError                                  Traceback (most recent call last)
~/miniconda2/envs/py37/lib/python3.7/site-packages/pandas/core/indexes/base.py in get_loc(self, key, method, tolerance)
   2656             try:
-> 2657                 return self._engine.get_loc(key)
   2658             except KeyError:

pandas/_libs/index.pyx in pandas._libs.index.IndexEngine.get_loc()

pandas/_libs/index.pyx in pandas._libs.index.IndexEngine.get_loc()

pandas/_libs/hashtable_class_helper.pxi in pandas._libs.hashtable.PyObjectHashTable.get_item()

pandas/_libs/hashtable_class_helper.pxi in pandas._libs.hashtable.PyObjectHashTable.get_item()

KeyError: 'Date'

During handling of the above exception, another exception occurred:

KeyError                                  Traceback (most recent call last)
<ipython-input-17-d55e44b68e87> in <module>
----> 1 df = dr.data.get_data_yahoo('FRE.DE',start='2018-10-1', end='2018-11-30')
      2 df

~/miniconda2/envs/py37/lib/python3.7/site-packages/pandas_datareader/data.py in get_data_yahoo(*args, **kwargs)
     68 
     69 def get_data_yahoo(*args, **kwargs):
---> 70     return YahooDailyReader(*args, **kwargs).read()
     71 
     72 

~/miniconda2/envs/py37/lib/python3.7/site-packages/pandas_datareader/base.py in read(self)
    208         if isinstance(self.symbols, (compat.string_types, int)):
    209             df = self._read_one_data(self.url,
--> 210                                      params=self._get_params(self.symbols))
    211         # Or multiple symbols, (e.g., ['GOOG', 'AAPL', 'MSFT'])
    212         elif isinstance(self.symbols, DataFrame):

~/miniconda2/envs/py37/lib/python3.7/site-packages/pandas_datareader/yahoo/daily.py in _read_one_data(self, url, params)
    140         prices.columns = [col.capitalize() for col in prices.columns]
    141         prices['Date'] = to_datetime(
--> 142             to_datetime(prices['Date'], unit='s').dt.date)
    143 
    144         if 'Data' in prices.columns:

~/miniconda2/envs/py37/lib/python3.7/site-packages/pandas/core/frame.py in __getitem__(self, key)
   2925             if self.columns.nlevels > 1:
   2926                 return self._getitem_multilevel(key)
-> 2927             indexer = self.columns.get_loc(key)
   2928             if is_integer(indexer):
   2929                 indexer = [indexer]

~/miniconda2/envs/py37/lib/python3.7/site-packages/pandas/core/indexes/base.py in get_loc(self, key, method, tolerance)
   2657                 return self._engine.get_loc(key)
   2658             except KeyError:
-> 2659                 return self._engine.get_loc(self._maybe_cast_indexer(key))
   2660         indexer = self.get_indexer([key], method=method, tolerance=tolerance)
   2661         if indexer.ndim > 1 or indexer.size > 1:

pandas/_libs/index.pyx in pandas._libs.index.IndexEngine.get_loc()

pandas/_libs/index.pyx in pandas._libs.index.IndexEngine.get_loc()

pandas/_libs/hashtable_class_helper.pxi in pandas._libs.hashtable.PyObjectHashTable.get_item()

pandas/_libs/hashtable_class_helper.pxi in pandas._libs.hashtable.PyObjectHashTable.get_item()

KeyError: 'Date'

当我将“ FRE.DE”替换为“ FRM.DE”时,这是一个不同的代码符号,它可以完美工作。

考虑,嗯,也许该符号的数据不存在,我转到了yahoo财务页面: https://de.finance.yahoo.com/quote/FRE.DE/history?p=FRE.DE

,并显示历史数据。

1 个答案:

答案 0 :(得分:0)

对我来说,您的代码实际上适用于FRE.DE,但不适用于FRM.DE

与以下事实一致

此页面: https://de.finance.yahoo.com/quote/FRE.DE 被发现

和此页面: https://de.finance.yahoo.com/quote/FRM.DE 找不到