How to specify date format when using pandas.to_csv?

user1642513 picture user1642513 · Dec 22, 2012 · Viewed 62.9k times · Source

The default output format of to_csv() is:

12/14/2012  12:00:00 AM

I cannot figure out how to output only the date part with specific format:

20121214

or date and time in two separate columns in the csv file:

20121214,  084530

The documentation is too brief to give me any clue as to how to do these. Can anyone help?

Answer

BubbleGuppies picture BubbleGuppies · Apr 2, 2014

Since version v0.13.0 (January 3, 2014) of Pandas you can use the date_format parameter of the to_csv method:

df.to_csv(filename, date_format='%Y%m%d')