Skip to content

Commit b482f1b

Browse files
committed
BUG: try fixing tzlocal bug
1 parent bf2c77b commit b482f1b

File tree

2 files changed

+21
-8
lines changed

2 files changed

+21
-8
lines changed

pandas/src/datetime.pyx

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -527,11 +527,11 @@ cdef class _TSObject:
527527
def __get__(self):
528528
return self.value
529529

530-
cpdef _get_utcoffset(tzinfo):
530+
cpdef _get_utcoffset(tzinfo, obj):
531531
try:
532532
return tzinfo._utcoffset
533533
except AttributeError:
534-
return tzinfo.utcoffset(None)
534+
return tzinfo.utcoffset(obj)
535535

536536
# helper to extract datetime and int64 from several different possibilities
537537
cpdef convert_to_tsobject(object ts, object tz=None):
@@ -570,20 +570,22 @@ cpdef convert_to_tsobject(object ts, object tz=None):
570570
ts = tz.normalize(ts)
571571
obj.value = _pydatetime_to_dts(ts, &obj.dts)
572572
obj.tzinfo = ts.tzinfo
573-
elif tz is not pytz.utc:
573+
elif not _is_utc(tz):
574574
ts = tz.localize(ts)
575575
obj.value = _pydatetime_to_dts(ts, &obj.dts)
576-
obj.value -= _delta_to_nanoseconds(_get_utcoffset(ts.tzinfo))
576+
offset = _get_utcoffset(ts.tzinfo, ts)
577+
obj.value -= _delta_to_nanoseconds(offset)
577578
obj.tzinfo = ts.tzinfo
578579
else:
579580
# UTC
580581
obj.value = _pydatetime_to_dts(ts, &obj.dts)
581-
obj.tzinfo = tz
582+
obj.tzinfo = pytz.utc
582583
else:
583584
obj.value = _pydatetime_to_dts(ts, &obj.dts)
584585
obj.tzinfo = ts.tzinfo
585586
if obj.tzinfo is not None and not _is_utc(obj.tzinfo):
586-
obj.value -= _delta_to_nanoseconds(_get_utcoffset(obj.tzinfo))
587+
offset = _get_utcoffset(obj.tzinfo, obj)
588+
obj.value -= _delta_to_nanoseconds()
587589
_check_dts_bounds(obj.value, &obj.dts)
588590
return obj
589591
elif PyDate_Check(ts):
@@ -1028,15 +1030,15 @@ def _get_deltas(tz):
10281030
# tzoffset not hashable in Python 3
10291031
hash(tz)
10301032
except TypeError:
1031-
num = int(total_seconds(_get_utcoffset(tz))) * 1000000000
1033+
num = int(total_seconds(_get_utcoffset(tz, None))) * 1000000000
10321034
return np.array([num], dtype=np.int64)
10331035

10341036
if tz not in utc_offset_cache:
10351037
if hasattr(tz, '_utc_transition_times'):
10361038
utc_offset_cache[tz] = _unbox_utcoffsets(tz._transition_info)
10371039
else:
10381040
# static tzinfo
1039-
num = int(total_seconds(_get_utcoffset(tz))) * 1000000000
1041+
num = int(total_seconds(_get_utcoffset(tz, None))) * 1000000000
10401042
utc_offset_cache[tz] = np.array([num], dtype=np.int64)
10411043
return utc_offset_cache[tz]
10421044

pandas/tseries/tests/test_timezones.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -414,6 +414,17 @@ def test_to_datetime_utc(self):
414414
result = to_datetime(arr, utc=True)
415415
self.assert_(result.tz is pytz.utc)
416416

417+
def test_to_datetime_tzlocal(self):
418+
from dateutil.parser import parse
419+
from dateutil.tz import tzlocal
420+
dt = parse('2012-06-13T01:39:00Z')
421+
dt = dt.replace(tzinfo = tzlocal())
422+
423+
arr = np.array([dt], dtype=object)
424+
425+
result = to_datetime(arr, utc=True)
426+
self.assert_(result.tz is pytz.utc)
427+
417428
def test_frame_no_datetime64_dtype(self):
418429
dr = date_range('2011/1/1', '2012/1/1', freq='W-FRI')
419430
dr_tz = dr.tz_localize('US/Eastern')

0 commit comments

Comments
 (0)