Skip to content Skip to sidebar Skip to footer

How To Change Datetime Format In Dataframe With Using Pandas?

I'm a pandas learner. I have a dataframe with the column 'DATE', the datetime format of the column is like '11/1/2017 1:00'. I want to change the datetime format from '11/1/2017 1:

Solution 1:

df=pd.DataFrame({'Time':[ '11/1/2017 1:00', '11/1/2017 1:00', '11/1/2017 1:00', '11/1/2017 1:00']})
df.Time=pd.to_datetime(df.Time).dt.strftime('%d-%b-%y %H:%M')
df
Out[870]: 
              Time
0  01-Nov-17 01:00
1  01-Nov-17 01:00
2  01-Nov-17 01:00
3  01-Nov-17 01:00

Solution 2:

The simple answer is that your format argument to the strptime function is wrong. You want datetime.strptime(x, "%m-%d-%Y %H:%M").

Also, make sure that all of your numbers are padded (i.e. for the month, make sure it is 01 for Jan instead of 1. Same idea for minutes and days), otherwise this may fail.

I would recommend you take a look at the python page for strptime formatting in order to learn more about how to format dates.

Post a Comment for "How To Change Datetime Format In Dataframe With Using Pandas?"