RPT
RPT

Reputation: 748

DateTime format in python

I am trying to convert a column in a dataFrame to a TD format. The column looks like this (they are currently strings):

01/11/2012 00:00    
01/11/2012 01:00    
01/11/2012 02:00

This is what I have done now:

df['Sdate'] =  pd.to_datetime(df['Sdate'], format='%d%m%y %H:%M.%f')

But, it throws an error saying,

time data '01/11/2012 00:00' does not match format '%d%m%y %H:%M.%f' (match)

Not, sure why it does that. Isn't HH:MM 00:00 - 23:00?

Or, am I missing something really stupid here?

Upvotes: 1

Views: 66

Answers (1)

jezrael
jezrael

Reputation: 862641

Use to_datetime with dayfirst parameter:

df['Sdate'] =  pd.to_datetime(df['Sdate'], dayfirst=True)
print (df)
                Sdate
0 2012-11-01 00:00:00
1 2012-11-01 01:00:00
2 2012-11-01 02:00:00

If want specify format:

df['Sdate'] =  pd.to_datetime(df['Sdate'], format='%d/%m/%Y %H:%M')
print (df)
                Sdate
0 2012-11-01 00:00:00
1 2012-11-01 01:00:00
2 2012-11-01 02:00:00

Upvotes: 2

Related Questions