0

I am reading a stock daily data into a panda:

dpa = pd.read_csv(fname,parse_dates=['Date']) print(dpa.head(5))

        Date   Open   High    Low  Close
0 2017-05-10  56.85  56.94  56.08  56.27
1 2017-05-11  56.13  56.38  55.80  56.23
2 2017-05-12  56.16  56.40  55.96  56.16
3 2017-05-15  56.07  56.76  56.01  56.70
4 2017-05-16  56.87  58.38  56.45  56.83

How can I use Date as the index and retrieve a column by specifying a date?

following didn't work:

 Close = dpa.Close['2017-05-12']

 Close = dpa['2017-05-12'].Close

I thought parse_dates = ['Date'] generates Date as index.

afshin
  • 1,783
  • 7
  • 22
  • 39
  • Either `dpa = dpa.set_index('Date')` or pass `index_col=0` or `index_col='Date'` while reading the csv. It will only parse the dates but will not set it as index if you don't specify the index. – ayhan May 14 '18 at 16:18
  • From the document *A fast-path exists for iso8601-formatted dates.* not set index ... – BENY May 14 '18 at 16:21
  • Thank you - index_col='Date' while reading the csv file worked as expected. – afshin May 14 '18 at 17:05

0 Answers0