I have this df:
ticker GSPC.INDX IBM.US US10Y.INDX adjustedRF index_returns equity_returns excess_market_returns alpha beta date 2013-11-04 1764.180033 179.116667 2.634467 0.001043 0.001218 -0.002539 0.000175 -0.001419 0.647420 2013-11-05 1767.130033 179.103333 2.643033 0.001047 0.001670 -0.000074 0.000624 -0.001423 0.647479 2013-11-06 1760.203333 179.013333 2.642467 0.001047 -0.003941 -0.000500 -0.004988 -0.001427 0.647593 2013-11-07 1762.750000 179.726667 2.668500 0.001057 0.001441 0.003987 0.000385 -0.001428 0.647597 2013-11-08 1763.216667 180.956667 2.701867 0.001070 0.000263 0.006795 -0.000807 -0.001428 0.647452 ... ... ... ... ... ... ... ... ... ... 2021-07-06 4351.336567 139.540000 1.368967 0.000542 0.002934 -0.016329 0.002392 -0.000276 0.391254 2021-07-07 4340.829900 139.780000 1.322733 0.000524 -0.002423 0.001710 -0.002947 -0.000276 0.391306 2021-07-08 4349.499833 140.693333 1.325433 0.000525 0.001990 0.006517 0.001465 -0.000275 0.391341 2021-07-09 4345.184800 141.130000 1.339500 0.000530 0.001308 0.006043 0.000782 -0.000276 0.391278 2021-07-10 4377.089850 141.220000 1.362000 0.000539 0.011215 0.005527 0.010676 -0.000271 0.391562 1992 rows × 9 columns
I had to pivot this table so I think ticker & date are indexes(?)
df.columns
results in:
Index(['GSPC.INDX', 'IBM.US', 'US10Y.INDX', 'adjustedRF', 'index_returns', 'equity_returns', 'excess_market_returns', 'alpha', 'beta'], dtype='object', name='ticker')
I wanted to create a new column for the year(I belive this does the trick – df['year'] = pd.to_datetime(df['date'],format='%Y')
) but it’s not finding the date column because I think it’s some kind of index?
How can I access it?
Advertisement
Answer
Your dataframe likely stores the dates in a DatetimeIndex. To access the year of each row in your dataframe, simply use df.index.year