Code Sample, a copy-pastable example if possible
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')
Problem description
The outcome datetime is in UTC when converting seconds, although utc option of pandas.to_datetime is defaultly set at None.
Expected Output
I expect to have an output in local time when utc option left as deflaut value. Also the utc option as True/False should change the to_datetime method output when it does not.
Output of pd.show_versions()
Comment From: TomAugspurger
The outcome datetime is in UTC when converting seconds, although utc option of pandas.to_datetime is defaultly set at None.
Can you clarify which output above is in UTC?
In [5]: pandas.to_datetime(s, unit='s').tz
I expect to have an output in local time when utc option left as deflaut value.
You're passing timezone-naive date times, so the output of to_datetime
is going to be timezone-naive asw ell.
Comment From: jreback
https://docs.python.org/2/library/time.html
mktime
returns a 'local' time (meaning in your local timezone). by-definition an epoch time is naive
.
you probably want this. using mktime
is going to be quite non-performant as well.
In [22]: pd.to_datetime( 1490889600.0, unit='s')
Out[22]: Timestamp('2017-03-30 16:00:00')
In [23]: pd.to_datetime( 1490889600.0, unit='s').tz_localize('UTC').tz_convert('US/Eastern')
Out[23]: Timestamp('2017-03-30 12:00:00-0400', tz='US/Eastern')
Comment From: DenisGDM
@TomAugspurger
In [34]: pandas.to_datetime('2017-03-30T12-00-00')
Out[34]: Timestamp('2017-03-30 12:00:00')
In [35]: pandas.to_datetime(1490868000.0, unit='s')
Out[35]: Timestamp('2017-03-30 10:00:00')
Out[34] is local time, when Out[35] is GMT time.
@jreback I have computed the number of second from 1970-01-01T00-00-00 to 2017-03-30T12-00-00 and found : 1490875200 = 1490868000.0 + 2*3600
In [27]: time.localtime(1490868000.0)
Out[27]: time.struct_time(tm_year=2017, tm_mon=3, tm_mday=30, tm_hour=12, tm_min=0, tm_sec=0, tm_wday=3, tm_yday=89, tm_isdst=1)
In [29]: time.gmtime(1490868000.0)
Out[29]: time.struct_time(tm_year=2017, tm_mon=3, tm_mday=30, tm_hour=10, tm_min=0, tm_sec=0, tm_wday=3, tm_yday=89, tm_isdst=0)
My mistake was in mktime
understanding, I should have used calendar.timegm
:
import pandas
import datetime, calendar
datetime1 = '2017-03-30T12-00-00'
d = datetime.datetime.strptime(datetime1, "%Y-%m-%dT%H-%M-%S")
s = calendar.timegm(d.timetuple())
print pandas.to_datetime(datetime1, format = "%Y-%m-%dT%H-%M-%S")
print pandas.to_datetime(s, unit='s')
The output are now the same.
Comment From: jreback
@DenisGDM to be honest I wouldn't use any of those stdlib functions, pandas does all of this in a vectorized natural way. (sure if you are doing it just a few times and it works for you great), but this will be slow for anything more.