I am trying to put the values of the index column of a dataframe into a variable as a list with the code
years = df.index
but it returns the error
'DatetimeIndex' object has no attribute 'index'
I have also tried using the name of the index column like df.Dates but I get the same error. The index column is in date format.
I have tried converting the dates into string using
df['Dates'] = df['Dates'].astype(str)
but I get the error
"only integers, slices (`:`), ellipsis (`...`), numpy.newaxis (`None`) and integer or boolean arrays are valid indices"
I would appreciate some help. I am new to pandas
You can get the data here:
startdate = '2022-01-01'
enddate = '2022-12-31'
stocks = pdr.DataReader('SPY','yahoo', startdate, enddate)[['Adj Close']]
bonds = pdr.DataReader('TLT','yahoo', startdate, enddate)[['Adj Close']]
oil = pdr.DataReader('USO','yahoo', startdate, enddate)[['Adj Close']]
df = pd.concat([stocks, bonds, oil], axis=1, join='inner')
df.columns = ['SPY','TLT','USO']