Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Getting KeyError : 'Date' in Yahoo #640

Closed
wavesailor opened this issue Jun 5, 2019 · 3 comments · Fixed by #697
Assignees

Comments

@wavesailor
Copy link

@wavesailor wavesailor commented Jun 5, 2019

This code works with most Ticker symbols:

from pandas_datareader import data as web
import datetime as dt

start = dt.datetime(2001, 1, 1)
end = dt.datetime(2016, 12, 31)

df = web.get_data_yahoo('CBS', start=start, end=end)

print(df.head())

But is does not work for these Ticker symbols (subset).
['BBT', CF', 'CTVA', 'COTY', 'DOW', 'EW', 'EVRG', 'HIG' ...]

I get the following error message.

    return self._engine.get_loc(key)
  File "pandas/_libs/index.pyx", line 108, in pandas._libs.index.IndexEngine.get_loc
  File "pandas/_libs/index.pyx", line 132, in pandas._libs.index.IndexEngine.get_loc
  File "pandas/_libs/hashtable_class_helper.pxi", line 1601, in pandas._libs.hashtable.PyObjectHashTable.get_item
  File "pandas/_libs/hashtable_class_helper.pxi", line 1608, in pandas._libs.hashtable.PyObjectHashTable.get_item
KeyError: 'Date'

I'm using python 3.6 on ubuntu with the following packages:

Package           Version 
----------------- --------
beautifulsoup4    4.7.1   
certifi           2019.3.9
chardet           3.0.4   
cycler            0.10.0  
idna              2.8     
joblib            0.13.2  
kiwisolver        1.1.0   
lxml              4.3.3   
matplotlib        3.1.0   
mpl-finance       0.10.0  
numpy             1.16.4  
pandas            0.24.2  
pandas-datareader 0.7.0   
pip               19.1.1  
pyparsing         2.4.0   
python-dateutil   2.8.0   
pytz              2019.1  
requests          2.22.0  
scikit-learn      0.21.2  
scipy             1.3.0   
setuptools        41.0.1  
six               1.12.0  
soupsieve         1.9.1   
urllib3           1.25.3  
wheel             0.33.4  
wrapt             1.11.1 
@b3l1nda

This comment has been minimized.

Copy link

@b3l1nda b3l1nda commented Jul 2, 2019

any solution for this ? i'm using python 3.7

@brandonsimpson21

This comment has been minimized.

Copy link

@brandonsimpson21 brandonsimpson21 commented Aug 8, 2019

I used a try except block and it worked. I have a list of stock tickers and my loop looks like:

for i in range(0,len(tickers)):
try:
x=data.DataReader(tickers[i],'yahoo',start_date,end_date)
x.to_excel(tickers[i]+'.xlsx')
except KeyError:
pass

@richard-bibb

This comment has been minimized.

Copy link

@richard-bibb richard-bibb commented Sep 17, 2019

I have the same problem. It seems to occur when a ticker is valid but data for the day doesn't exist. This results in an empty data frame being returned. A quick fix in the codebase itself is to catch KeyError on line 228 of base.py (inside function _dl_mult_symbols()

   def _dl_mult_symbols(self, symbols):
        stocks = {}
        failed = []
        passed = []
        for sym_group in _in_chunks(symbols, self.chunksize):
            for sym in sym_group:
                try:
                    stocks[sym] = self._read_one_data(self.url,
                                                      self._get_params(sym))
                    passed.append(sym)
                except (IOError, **KeyError**):
                    msg = 'Failed to read symbol: {0!r}, replacing with NaN.'
                    warnings.warn(msg.format(sym), SymbolWarning)
                    failed.append(sym)



@bashtage bashtage self-assigned this Sep 18, 2019
bashtage added a commit to bashtage/pandas-datareader that referenced this issue Sep 18, 2019
Catch empty DataFrame and report a warning

closes pydata#640
bashtage added a commit to bashtage/pandas-datareader that referenced this issue Sep 18, 2019
Catch empty DataFrame and report a warning

closes pydata#640
bashtage added a commit to bashtage/pandas-datareader that referenced this issue Sep 18, 2019
Catch empty DataFrame and report a warning

closes pydata#640
@bashtage bashtage mentioned this issue Sep 18, 2019
2 of 2 tasks complete
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
5 participants
You can’t perform that action at this time.