0

I am trying to set time index to pandas DataFrame using pandas.to_datetime function, but the outcome datetime is UTC when converting seconds, and while it is not requested :

import pandas
import datetime,time

datetime1 = '2017-03-30T12-00-00'
d = datetime.datetime.strptime(datetime1, "%Y-%m-%dT%H-%M-%S")
s = time.mktime(d.timetuple())

print pandas.to_datetime(datetime1, format = "%Y-%m-%dT%H-%M-%S")
print pandas.to_datetime(s, unit='s')

Get two different results, although utc option of pandas.to_datetime is not used in both cases.

Any ideas ?

dgdm
  • 85
  • 1
  • 2

1 Answers1

0

time.mktime() does not return UTC by default, see the docs for time.mktime():

Its argument is the struct_time or full 9-tuple [...] which expresses the time in local time, not UTC.

acidtobi
  • 1,375
  • 9
  • 13
  • Yes, `time.mktime()` return local time. But once in `pandas.to_datetime`, the pandas outcome is UTC. – dgdm Apr 03 '17 at 14:09
  • My mistake was in `mktime` understanding, I should have used `calendar.timegm` : `s = calendar.timegm(d.timetuple())` – dgdm Apr 05 '17 at 12:57