William William - 1 year ago 65
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:


When I use
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 Source

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')
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download