This code works with most Ticker symbols:
````python
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.
python
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
````
any solution for this ? i'm using python 3.7
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
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)
I received the 'Keyerror 'Date' when using pandas datareader' error and found two errors in my script that fixed the issue:
The name of the entity was incorrect, for example using 'APPL' instead of 'AAPL'.
There was no data for the date parameters I was using.
Hope this helps!
I've been having the same problem, but only when I deploy to heroku. Everything works fine locally... I've tried @brandonsimpson21 method but no luck.
def get_data(ticker, start, end):
try:
stock_data = data.DataReader(ticker, 'yahoo', start, end)
return stock_data
except RemoteDataError:
pass
2020-05-31T03:40:27.831064+00:00 app[web.1]: File "pandas/_libs/index.pyx", line 111, in pandas._libs.index.IndexEngine.get_loc
2020-05-31T03:40:27.831064+00:00 app[web.1]: File "pandas/_libs/index.pyx", line 138, in pandas._libs.index.IndexEngine.get_loc
2020-05-31T03:40:27.831065+00:00 app[web.1]: File "pandas/_libs/hashtable_class_helper.pxi", line 1619, in pandas._libs.hashtable.PyObjectHashTable.get_item
2020-05-31T03:40:27.831065+00:00 app[web.1]: File "pandas/_libs/hashtable_class_helper.pxi", line 1627, in pandas._libs.hashtable.PyObjectHashTable.get_item
2020-05-31T03:40:27.831066+00:00 app[web.1]: KeyError: 'Date'
Most helpful comment
I've been having the same problem, but only when I deploy to heroku. Everything works fine locally... I've tried @brandonsimpson21 method but no luck.
def get_data(ticker, start, end): try: stock_data = data.DataReader(ticker, 'yahoo', start, end) return stock_data except RemoteDataError: pass2020-05-31T03:40:27.831064+00:00 app[web.1]: File "pandas/_libs/index.pyx", line 111, in pandas._libs.index.IndexEngine.get_loc 2020-05-31T03:40:27.831064+00:00 app[web.1]: File "pandas/_libs/index.pyx", line 138, in pandas._libs.index.IndexEngine.get_loc 2020-05-31T03:40:27.831065+00:00 app[web.1]: File "pandas/_libs/hashtable_class_helper.pxi", line 1619, in pandas._libs.hashtable.PyObjectHashTable.get_item 2020-05-31T03:40:27.831065+00:00 app[web.1]: File "pandas/_libs/hashtable_class_helper.pxi", line 1627, in pandas._libs.hashtable.PyObjectHashTable.get_item 2020-05-31T03:40:27.831066+00:00 app[web.1]: KeyError: 'Date'