I am trying to run this code:
from pandas_datareader import data as dreader
from datetime import datetime
ticker = ["SPY","GOOG"]
all_historic_data = pd.DataFrame()
for i in ticker:
ticker_data = dreader.DataReader(i,'yahoo','1985-01-01',datetime.now().strftime("%Y-%m-%d"))
ticker_data_clean = ticker_data[["Date", "Close", "High", "Low"]]
ticker_data_clean["date"] = pd.to_datetime(ticker_data_clean["Date"])
ticker_data_clean.insert(1, "ticker", t)
ticker_data_clean["return"] = ticker_data_clean["Close"].pct_change()
ticker_data_clean["range"] = (ticker_data_clean["High"]/ticker_data_clean["Low"])-1
del ticker_data_clean["High"]
del ticker_data_clean["Low"]
ticker_data_clean.dropna(how="any", inplace=True)
print(ticker_data.tail())
and getting an error on the 2nd line of the loop
> KeyError Traceback (most recent call
> last) <ipython-input-51-95844c108758> in <module>()
> 8 for i in ticker:
> 9 ticker_data = dreader.DataReader(i,'yahoo','1985-01-01',datetime.now().strftime("%Y-%m-%d"))
> ---> 10 ticker_data_clean = ticker_data[["Date", "Close", "High", "Low"]]
> 11 ticker_data_clean["date"] = pd.to_datetime(ticker_data_clean["Date"])
> 12 ticker_data_clean.insert(1, "ticker", t)
>
> 3 frames
> /usr/local/lib/python3.6/dist-packages/pandas/core/indexing.py in
> _validate_read_indexer(self, key, indexer, axis, raise_missing) 1250 if not(self.name == 'loc' and not raise_missing):
> 1251 not_found = list(set(key) - set(ax))
> -> 1252 raise KeyError("{} not in index".format(not_found)) 1253 1254 # we skip the
> warning on Categorical/Interval
>
> KeyError: "['Date'] not in index"
ticker_data.columns
returns: Index(['High', 'Low', 'Open', 'Close', 'Volume', 'Adj Close'], dtype='object')
and
print(ticker_data.tail())
returns:
High Low ... Volume Adj Close
Date ...
2019-10-09 292.299988 290.059998 ... 62359400.0 291.269989
2019-10-10 294.209991 291.000000 ... 55296300.0 293.239990
2019-10-11 298.739990 296.140015 ... 98720400.0 296.279999
2019-10-14 296.670013 295.570007 ... 40394800.0 295.950012
2019-10-15 299.700012 296.970001 ... 31632183.0 299.239990
I could not find a way to remove/reset the Date index Pls advice. Thanks [5 rows x 6 columns]