Aug-03-2021, 08:56 PM
Dear users,
I am getting an IndexError, and not sure why? The code (below) works perfectly when I run it using data.cvs. On that type of file, the index is a column (named Date).
But, if I run the code directly from an input library (e.i., df = yf.download(tickers=ticker, start=start, end=end), I receive the trackback message's below. In this example, the date is not considered a column. So, I think the error comes from that; however, I don't know how to fix it.
I hope I am being clear. It is my first post, and I am a beginner.
Thank you
This is my Code:
I am getting an IndexError, and not sure why? The code (below) works perfectly when I run it using data.cvs. On that type of file, the index is a column (named Date).
But, if I run the code directly from an input library (e.i., df = yf.download(tickers=ticker, start=start, end=end), I receive the trackback message's below. In this example, the date is not considered a column. So, I think the error comes from that; however, I don't know how to fix it.
I hope I am being clear. It is my first post, and I am a beginner.
Thank you
This is my Code:
df_close_adj = pd.DataFrame(data=df[["Close", "Adj_Close"]].to_numpy(), index=df["Date"], columns=[["Close", "Adj_Close"]])The Traceback I get is this:
Error:---------------------------------------------------------------------------
KeyError Traceback (most recent call last)
/usr/local/lib/python3.7/dist-packages/pandas/core/indexes/base.py in get_loc(self, key, method, tolerance)
2897 try:
-> 2898 return self._engine.get_loc(casted_key)
2899 except KeyError as err:
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'
The above exception was the direct cause of the following exception:
KeyError Traceback (most recent call last)
2 frames
/usr/local/lib/python3.7/dist-packages/pandas/core/indexes/base.py in get_loc(self, key, method, tolerance)
2898 return self._engine.get_loc(casted_key)
2899 except KeyError as err:
-> 2900 raise KeyError(key) from err
2901
2902 if tolerance is not None:
KeyError: 'Date'