1

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?

1
  • 2
    You're missing /, you don't need a microsecond directive, and %y should be %Y; instead use '%d/%m/%Y %H:%M'. Commented Nov 4, 2017 at 12:22

1 Answer 1

2

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
Sign up to request clarification or add additional context in comments.

2 Comments

Exactly what I was looking for. Thanks. Can you explain what effect the 'dayfirst=True' has?
If omit it it will be parsed 2012-01-11 00:00:00 if all dates are not claer like 2012-11-21 00:00:00. So it can be obviusly omit, but if only a few dates and not clear DDMMYY vs MMDDYY it is helpful.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.