Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Convert Pandas column object to date

I’m trying to convert all the dates in a pandas column from object to date.
The object column is called PRODUCT_CREATION_DATE, and has values like:

2021-12-09 00:00:00-05:00

2021-04-20 00:00:00-07:00

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

2017-04-25 00:00:00-07:00

I already know I can convert them with pd.to_datetime, but I don’t seem to be able to find the correct format, and I keep getting a format error. The problem is the last part, I have no idea what those -05:00 or -07:00 should represent. I actually only care about year, month and day, so if there’s a way to get a date with only year, month and day, it would be even better….

This is the code I’m trying to use:

pd.to_datetime(df_info['PRODUCT_CREATION_DATE'], format='%Y-%m-%d %H:%M:%S-%f')

This returns

ValueError: unconverted data remains: :00

>Solution :

The code %f is used for microseconds, whereas in your sample dates, -05:00 refers to the UTC timezone offset. So, the format you need to use with to_datetime will be

%Y-%m-%d %H:%M:%S%z

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading