"Date" Column heading in downloaded securities data

When downloading securities data from Yahoo, the "Date"  header appears on a seperate line from the other headers. As a result I am unable to plot, for example, Date vs. Close.

I receive the error message:

KeyError: "['Date'] not in index"

Hi Michael,



The error message you are getting is because the "Date" header is not included in the index of the DataFrame. The index is a list of the column names, and it is used to look up data in the DataFrame. When you try to plot "Date" vs. Close, Python is looking for the "Date" column in the index, but it is not there. To fix this, you need to add the "Date" header to the index. You can do this by using the set_index() method on the DataFrame (df = df.set_index("Date")) and then plot it.



Feel free to reach out if you have any further queries.

I tried your suggestion and this is the error that I got:

---------------------------------------------------------------------------
KeyError                                  Traceback (most recent call last)
<ipython-input-9-5c181b937004> in <cell line: 1>()
----> 1 BA_df = BA_df.set_index("Date")


1 frames
/usr/local/lib/python3.10/dist-packages/pandas/core/frame.py in set_index(self, keys, drop, append, inplace, verify_integrity)
   6010 
   6011         if missing:
-> 6012             raise KeyError(f"None of {missing} are in the columns")
   6013 
   6014         if inplace:
KeyError: "None of ['Date'] are in the columns"

Hi Michael,



The "Date" column is already the index of the dataframe. You can plot the data using the following lines of code:

 

import yfinance as yf
import matplotlib.pyplot as plt

data = yf.download("AAPL", start="2022-01-01", end="2023-01-01")

plt.figure(figsize=(10, 6))
plt.plot(data['Close'])
plt.title("Stock Price")
plt.xlabel("Date")
plt.ylabel("Close Price")
plt.show()

Hope this helps!

Thanks,
Akshay