简体   繁体   中英

Copy data-frame date index into a list

I have a data-frame df with a head that looks like:

                                 amount_1  ...  factor
date                                       ...
2014-04-21                     931.550533  ...   100.0
2014-04-22                     932.174195  ...   100.0
2014-04-23                     946.622481  ...   100.0
2014-04-24                     938.722707  ...   100.0
2014-04-25                     949.325041  ...   100.0

I would like to take the index ( date ) and create a list. I am using:

dates = df.index.map(pd.Timestamp.date).tolist()

which gives me an output:

[datetime.date(2014, 2, 21), datetime.date(2014, 2, 22), datetime.date(2014, 2, 23), datetime.date(2014, 2, 24), datetime.date(2014, 2, 25), .....]

However I would like the output to look like:

[2014-04-21,2014-04-22,2014-04-23,2014-04-24,2014-04-25,....]

How can I do this?

If want YYYY-MM-DDDD in strings use DatetimeIndex.strftime :

dates = df.index.strftime('%Y-%m-Yd')

If want remove times, it means set to 00:00:00 use DatetimeIndex.floor :

dates = df.index.floor('d')

Your solution should be changed use DatetimeIndex.date :

dates = df.index.date

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM