DeeeeRoy
DeeeeRoy

Reputation: 487

Pandas Timestamp index to list of date strings

I have the following index values in a pandas dataframe.

df.index[0:5]

DatetimeIndex(['2004-05-31', '2004-06-30', '2004-07-31', '2004-08-31',
           '2004-09-30'],
          dtype='datetime64[ns]', name='as_of_dt', freq=None)

How can I convert them into a list of like this:

['5/31/2004','6/30/2004','7/31/2004','8/31/2004']

Upvotes: 9

Views: 9054

Answers (2)

user6903745
user6903745

Reputation: 5527

List comprehension and built-in string conversion may work:

[str(t) for t in df.index]

Upvotes: 0

Andy Hayden
Andy Hayden

Reputation: 375865

You can use strftime:

In [11]: df.index.strftime("%Y/%m/%d")
Out[11]:
array(['2004/05/31', '2004/06/30', '2004/07/31', '2004/08/31', '2004/09/30'],
      dtype='<U10')

Upvotes: 13

Related Questions