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()

INSTALLED VERSIONS ------------------ commit: None python: 2.7.11.final.0 python-bits: 64 OS: Linux OS-release: 3.10.0-514.10.2.el7.x86_64 machine: x86_64 processor: x86_64 byteorder: little LC_ALL: None LANG: fr_FR.UTF-8 pandas: 0.17.1 nose: 1.3.7 pip: 9.0.1 setuptools: 30.3.0 Cython: 0.22.1 numpy: 1.10.1 scipy: 0.15.1 statsmodels: None IPython: 5.1.0 sphinx: 1.3.4 patsy: None dateutil: 2.4.2 pytz: 2015.7 blosc: None bottleneck: None tables: None numexpr: 2.4.6 matplotlib: 1.4.3 openpyxl: 2.3.4 xlrd: 0.9.4 xlwt: 1.0.0 xlsxwriter: None lxml: None bs4: None html5lib: None httplib2: None apiclient: None sqlalchemy: 0.8.2 pymysql: None psycopg2: 2.6.1 (dt dec pq3 ext) Jinja2: None

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.