lapally
lapally

Reputation: 133

Changing date format in python

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!

Upvotes: 2

Views: 4323

Answers (2)

MaxU - stand with Ukraine
MaxU - stand with Ukraine

Reputation: 210982

Try this:

df['Date'] = pd.to_datetime(df['Data'], format='%Y/%d/%m %H:%M:%S')

Upvotes: 1

Jason Callahan
Jason Callahan

Reputation: 525

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().

Upvotes: 0

Related Questions