I have a specific case in which i get an KeyError, that seemed to me not yet solved at Stackoverflow.
Here is the Code that i use
import pandas as pd
import pandas_datareader as web
import matplotlib.pyplot as plt
%matplotlib inline
portfolio = ["FB", "AMZN", "APPL", "NFLX", "GOOG" ]
start = "2018-01-01"
end = "2020-01-01"
df = pd.DataFrame()
for asset in portfolio:
df[asset] = web.DataReader(asset, data_source = "yahoo", start = start, end = end)["Adj Close"]
df
and here is the Error Message
KeyError Traceback (most recent call last)
~\anaconda3\lib\site-packages\pandas\core\indexes\base.py in get_loc(self, key, method, tolerance)
2645 try:
-> 2646 return self._engine.get_loc(key)
2647 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-12-cb7c572224e0> in <module>
13
14 for asset in portfolio:
---> 15 df[asset] = web.DataReader(asset, data_source = "yahoo", start = start, end = end)["Adj Close"]
16
17 df
~\anaconda3\lib\site-packages\pandas\util\_decorators.py in wrapper(*args, **kwargs)
212 else:
213 kwargs[new_arg_name] = new_arg_value
--> 214 return func(*args, **kwargs)
215
216 return cast(F, wrapper)
~\anaconda3\lib\site-packages\pandas_datareader\data.py in DataReader(name, data_source, start, end, retry_count, pause, session, api_key)
376 retry_count=retry_count,
377 pause=pause,
--> 378 session=session,
379 ).read()
380
~\anaconda3\lib\site-packages\pandas_datareader\base.py in read(self)
251 # If a single symbol, (e.g., 'GOOG')
252 if isinstance(self.symbols, (string_types, int)):
--> 253 df = self._read_one_data(self.url, params=self._get_params(self.symbols))
254 # Or multiple symbols, (e.g., ['GOOG', 'AAPL', 'MSFT'])
255 elif isinstance(self.symbols, DataFrame):
~\anaconda3\lib\site-packages\pandas_datareader\yahoo\daily.py in _read_one_data(self, url, params)
159 prices = DataFrame(data["prices"])
160 prices.columns = [col.capitalize() for col in prices.columns]
--> 161 prices["Date"] = to_datetime(to_datetime(prices["Date"], unit="s").dt.date)
162
163 if "Data" in prices.columns:
~\anaconda3\lib\site-packages\pandas\core\frame.py in __getitem__(self, key)
2798 if self.columns.nlevels > 1:
2799 return self._getitem_multilevel(key)
-> 2800 indexer = self.columns.get_loc(key)
2801 if is_integer(indexer):
2802 indexer = [indexer]
~\anaconda3\lib\site-packages\pandas\core\indexes\base.py in get_loc(self, key, method, tolerance)
2646 return self._engine.get_loc(key)
2647 except KeyError:
-> 2648 return self._engine.get_loc(self._maybe_cast_indexer(key))
2649 indexer = self.get_indexer([key], method=method, tolerance=tolerance)
2650 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'
what am i doing wrong here?
It seems like he programmed lines are very simple and i assume that the issue is not in here. Other solutions that were presented at Stackoverflow seemed to be very specific too, so that i cannot really derive from that.
Thanks guys, i appreciate!
Benjamin