Home > database >  How to get datetime from a pandas dataframe by row index
How to get datetime from a pandas dataframe by row index

Time:08-07

How to get the date from this following dataframe by row index? I am looking for a function something like:

get_datetime(dataframe, row_index = 1)

which will return the datetime as 2022-01-11

I have read the Time series / date functionality of Pandas, however, did not see anything useful there.

             open   high    low  close    volume  dividends  stock splits
Date                                                                     
2022-01-06  21.99  22.22  21.59  22.18   1956300          0             0
2022-01-11  22.32  22.93  22.23  22.92   1885700          0             0
2022-01-21  22.40  22.55  22.02  22.13   2265500          0             0
2022-01-26  22.28  22.72  21.79  22.05   2247600          0             0
2022-01-31  22.02  22.92  21.72  22.86   2955500          0             0
2022-02-10  24.73  25.51  24.59  25.29   4069300          0             0
2022-02-15  24.85  25.57  24.56  25.47   5661100          0             0
2022-02-25  24.59  25.07  24.46  24.67   3722400          0             0
2022-03-02  23.20  23.95  23.15  23.82   3268100          0             0
2022-03-07  21.54  21.60  19.51  19.59  10102600          0             0

CodePudding user response:

Do something like this to get it just as the date without the timestamp:

df.index[0].date()

prints: 2022-01-06

CodePudding user response:

You can access the index with df.index

df.index[1]

If your index is datetime, you can use DatetimeIndex.date to get the datetime.date objects

df.index.date[1]

Or with DatetimeIndex.strftime to get string format

df.index.strftime('%Y-%m-%d')[1]
# use `str` for convenience if the format is %Y-%m-%d
str(df.index.date[1])
  • Related