William William - 2 months ago 10
Python Question

How do I get .to_datetime() to stop offsetting by UTC in string

I'm converting a timestamp given as a string from a .CSV file. It is in this format:

2016-06-06T09:30:00.000856632-04:00


When I use
.to_datetime()
to convert it to a date object, it offsets the UTC time. In this example, 9:30AM becomes 1:30PM:

2016-06-06 13:30:00.000856632


I read the documentation for the function and thought setting UTC = False as a parameter would fix this but it just offsets the time by a different amount.

Answer

The string has a -4 hours offset. Remove that before conversion:

>>> pd.to_datetime("2016-06-06T09:30:00.000856632-04:00"[:-6])
Timestamp('2016-06-06 09:30:00.000856632')
Comments