2

I have a pandas dataframe with a column containing a date; the format of the original string is YYYY/DD/MM HH:MM:SS. I am trying to convert the string into a datetime format, by using

df['Date']=pd.to_datetime(df['Data'], errors='coerce')

but plotting it I can see it doesn't recognize the correct format. Can you help me to understand whether there is an option to give python the correct format to read the column? I have seen the format tag for to_datetime function, but I can't use it correctly.

Thanks a lot for your help!

1
  • can you post the output of print(df.dtypes) __after __ the call of pd.to_datetime(...)? Commented Jul 15, 2016 at 15:41

2 Answers 2

1

Try this:

df['Date'] = pd.to_datetime(df['Data'], format='%Y/%d/%m %H:%M:%S')
Sign up to request clarification or add additional context in comments.

Comments

0

It looks like you're using a non-standard date format. It should be YYYY-MM-DD. Try formating with the strptime() method.

time.strptime('2016/15/07', '%Y/%d/%m')

If you need to get it to a string after that use time.strftime().

1 Comment

Thanks a lot! Can I also deal with time with this method?

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.