caddie
caddie

Reputation: 169

Pandas DateTime Formating

I'm trying to figure out how to reformat a date in a pandas df. The source date is a string in the format of %Y-%m-%d %H:%M:%S. I use the following code to convert it do a date field in a format=%Y-%m-%d.

df['Date'] = pd.to_datetime(df['Date'], format='%Y-%m-%d').dt.date

How do I convert a date field in the format of %Y-%m-%d into %m/%d/%Y ?

Thanks!

Upvotes: 1

Views: 42

Answers (1)

mohsinali
mohsinali

Reputation: 296

i think this should help

 df['Date'] = pd.to_datetime(df['Date'], format='%Y-%m-%d').dt.date
 df['Date'] = df['Date'].dt.strftime('%m/%d/%Y')

you can try a one liner

 df['Date'] = pd.to_datetime(df['Date'], format='%Y-%m-%d').dt.strftime('%m/%d/%Y')

Upvotes: 1

Related Questions