How to specify date format when using pandas.to_csv?
Question:
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?
Answers:
You could use strftime
to save these as separate columns:
df['date'] = df['datetime'].apply(lambda x: x.strftime('%d%m%Y'))
df['time'] = df['datetime'].apply(lambda x: x.strftime('%H%M%S'))
and then be specific about which columns to export to csv:
df[['date', 'time', ... ]].to_csv('df.csv')
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')
To export as a timestamp, do this:
df.to_csv(filename, date_format='%s')
The %s
format is not documented in python/pandas but works in this case.
I found the %s
from the dates formats of ruby. Strftime doc for C here
Note that the timestamp miliseconds format %Q
does not work with pandas (you’ll have a litteral %Q
in the field instead of the date). I caried my sets with python 3.6 and pandas 0.24.1
Non of these timestamp formats seem to work for me.
Tried:
from datetime import datetime
ordersUnleashed.to_csv('sendtoUnleashed.csv', date_format='%s.%f')
No luck. Even tried pip install datetime.
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?
You could use strftime
to save these as separate columns:
df['date'] = df['datetime'].apply(lambda x: x.strftime('%d%m%Y'))
df['time'] = df['datetime'].apply(lambda x: x.strftime('%H%M%S'))
and then be specific about which columns to export to csv:
df[['date', 'time', ... ]].to_csv('df.csv')
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')
To export as a timestamp, do this:
df.to_csv(filename, date_format='%s')
The %s
format is not documented in python/pandas but works in this case.
I found the %s
from the dates formats of ruby. Strftime doc for C here
Note that the timestamp miliseconds format %Q
does not work with pandas (you’ll have a litteral %Q
in the field instead of the date). I caried my sets with python 3.6 and pandas 0.24.1
Non of these timestamp formats seem to work for me.
Tried:
from datetime import datetime
ordersUnleashed.to_csv('sendtoUnleashed.csv', date_format='%s.%f')
No luck. Even tried pip install datetime.