2

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.

William
  • 59
  • 8

3 Answers3

2

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')
coder.in.me
  • 1,048
  • 9
  • 19
1

You can subtract the offset of 4 hours with timedelta after the conversion:

import datetime
import pandas as pd
pd.to_datetime("2016-06-06T09:30:00.000856632-04:00") + datetime.timedelta(hours = -4)
# Timestamp('2016-06-06 09:30:00.000856632')
Psidom
  • 209,562
  • 33
  • 339
  • 356
0

Pandas .to_datetime() has a format argument.

format : string, default None

strftime to parse time, eg “%d/%m/%Y”, note that “%f” will parse all the way up to nanoseconds.

.to_datetime(format="%d/%m/%Y")

Will yield

'11/03/2016'

The format follows the same Python datetime format that is used in .strftime(). For a complete list of time format parameters, see this reference.

Soviut
  • 88,194
  • 49
  • 192
  • 260