There is an issue with data from yfinance (it has multicolumn)
Price Adj Close Close ... Open Volume
Ticker SPY SPY ... SPY SPY
Date ...
2022-01-03 00:00:00+00:00 458.720367 477.709991 ... 476.299988 72668200
2022-01-04 00:00:00+00:00 458.566650 477.549988 ... 479.220001 71178700
There is an updated function
# Fetch historical SPY data
def fetch_data(symbol, start_date, end_date):
data = yf.download(symbol, start=start_date, end=end_date)
if isinstance(data.columns, pd.MultiIndex) and data.columns.nlevels > 1:
data.columns = data.columns.droplevel(1)
return data