0

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?

halfer
  • 19,824
  • 17
  • 99
  • 186
Stacey
  • 4,825
  • 17
  • 58
  • 99
  • Does this answer your question? [Convert datetime object to a String of date only in Python](https://stackoverflow.com/questions/10624937/convert-datetime-object-to-a-string-of-date-only-in-python) – AMC Apr 24 '20 at 00:24

1 Answers1

1

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
jezrael
  • 822,522
  • 95
  • 1,334
  • 1,252