Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to access index column of pandas pivot table

Tags:

python

pandas

I would like to access the Date/index column of a pandas pivot table, then use the date as matplotlib axis to plot stock data in an object oriented manner, I am trying figure out how I might get the date into its own object? Thank You

import pandas as pd
import yfinance as yf
amazon = yf.download('amzn','2000-01-01','2019-09-26')
amazon.head()

            Open    High    Low Close   Adj Close   Volume
Date                        
2000-01-03  81.50   89.56   79.05   89.38   89.38   16117600
2000-01-04  85.38   91.50   81.75   81.94   81.94   17487400
2000-01-05  70.50   75.12   68.00   69.75   69.75   38457400
2000-01-06  71.31   72.69   64.00   65.56   65.56   18752000
2000-01-07  67.00   70.50   66.19   69.56   69.56   10505400

like image 751
bryan Avatar asked Sep 02 '25 10:09

bryan


1 Answers

You can use amazon.index:

print (amazon.index)
DatetimeIndex(['1999-12-31', '2000-01-03', '2000-01-04', '2000-01-05',
               '2000-01-06', '2000-01-07', '2000-01-10', '2000-01-11',
               '2000-01-12', '2000-01-13',
               ...
               '2019-09-12', '2019-09-13', '2019-09-16', '2019-09-17',
               '2019-09-18', '2019-09-19', '2019-09-20', '2019-09-23',
               '2019-09-24', '2019-09-25'],
              dtype='datetime64[ns]', name='Date', length=4965, freq=None)

But if plot in pandas, e.g. by Series.plot not necessary:

amazon['Open'].plot()
like image 126
jezrael Avatar answered Sep 05 '25 00:09

jezrael