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
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