2

The OP of this post highlighted the lack of documentation for using the date_format argument in pandas. I would like to format the data into YYYY-MM-DD, not MM/DD/YYYY when using the to_csv command.

I've tried: frame.to_csv(output_dir, index=False, encoding='utf-8', date_format='%Y-%m-%d') and it is not working, where frame is a DataFrame

The file I'm trying to convert can be found here

Community
  • 1
  • 1
NumenorForLife
  • 1,736
  • 8
  • 27
  • 55

3 Answers3

3

This is already supported in to_csv as a param date_format:

t="""date,val
12/05/1984,sadas
1/11/1980,sadas
2/4/1945,sadas
22/10/1921,sadas"""
df = pd.read_csv(io.StringIO(t), parse_dates=[0])
df.to_csv(r'c:\data\date.csv', date_format='%Y-%m-%d')

Yields:

,date,val
0,1984-12-05,sadas
1,1980-01-11,sadas
2,1945-02-04,sadas
3,1921-10-22,sadas

The format strings are of the form taken from strftime

EdChum
  • 376,765
  • 198
  • 813
  • 562
1

You can use the standard datetime string formatting behavior, here that would be: df.to_csv('df.csv, date_format="%Y-%m-%d") https://docs.python.org/2/library/datetime.html#strftime-strptime-behavior

EDIT: don't iterate through the dataframe with strftime, that's highly unoptimized and theres already a built-in function for performing the operation you want

0

You show iterate csv' rows and get every date and format it with strptime method from datetime.

Gocht
  • 9,924
  • 3
  • 42
  • 81