Skip to content

Commit b99f075

Browse files
stephenwlinchanghiskhan
authored andcommitted
BUG: Timestamp constructor not handling timezone conversions correctly
1 parent 892634a commit b99f075

File tree

2 files changed

+33
-11
lines changed

2 files changed

+33
-11
lines changed

pandas/tests/test_series.py

+22
Original file line numberDiff line numberDiff line change
@@ -2545,6 +2545,7 @@ def test_asof(self):
25452545
self.assert_(np.isnan(self.ts.asof(d)))
25462546

25472547
def test_getitem_setitem_datetimeindex(self):
2548+
from pytz import timezone as tz
25482549
from pandas import date_range
25492550
N = 50
25502551
# testing with timezone, GH #2785
@@ -2598,6 +2599,27 @@ def test_getitem_setitem_datetimeindex(self):
25982599
result["1990-01-02"] = ts[24:48]
25992600
assert_series_equal(result, ts)
26002601

2602+
# also test Timestamp tz handling, GH #2789
2603+
result = ts.copy()
2604+
result["1990-01-01 09:00:00+00:00"] = 0
2605+
result["1990-01-01 09:00:00+00:00"] = ts[4]
2606+
assert_series_equal(result, ts)
2607+
2608+
result = ts.copy()
2609+
result["1990-01-01 03:00:00-06:00"] = 0
2610+
result["1990-01-01 03:00:00-06:00"] = ts[4]
2611+
assert_series_equal(result, ts)
2612+
2613+
result = ts.copy()
2614+
result[datetime(1990, 1, 1, 9, 0, 0, tzinfo=tz('UTC'))] = 0
2615+
result[datetime(1990, 1, 1, 9, 0, 0, tzinfo=tz('UTC'))] = ts[4]
2616+
assert_series_equal(result, ts)
2617+
2618+
result = ts.copy()
2619+
result[datetime(1990, 1, 1, 3, 0, 0, tzinfo=tz('US/Central'))] = 0
2620+
result[datetime(1990, 1, 1, 3, 0, 0, tzinfo=tz('US/Central'))] = ts[4]
2621+
assert_series_equal(result, ts)
2622+
26012623
def test_getitem_setitem_periodindex(self):
26022624
from pandas import period_range, Period
26032625
N = 50

pandas/tslib.pyx

+11-11
Original file line numberDiff line numberDiff line change
@@ -616,27 +616,26 @@ cdef convert_to_tsobject(object ts, object tz):
616616
if tz is not None:
617617
# sort of a temporary hack
618618
if ts.tzinfo is not None:
619-
if hasattr(tz, 'normalize'):
619+
if (hasattr(tz, 'normalize') and
620+
hasattr(ts.tzinfo, '_utcoffset')):
620621
ts = tz.normalize(ts)
621622
obj.value = _pydatetime_to_dts(ts, &obj.dts)
622623
obj.tzinfo = ts.tzinfo
623624
else: #tzoffset
624-
ts_offset = _get_utcoffset(ts.tzinfo, ts)
625625
obj.value = _pydatetime_to_dts(ts, &obj.dts)
626+
ts_offset = _get_utcoffset(ts.tzinfo, ts)
626627
obj.value -= _delta_to_nanoseconds(ts_offset)
627628
tz_offset = _get_utcoffset(tz, ts)
628629
obj.value += _delta_to_nanoseconds(tz_offset)
629-
630+
pandas_datetime_to_datetimestruct(obj.value,
631+
PANDAS_FR_ns, &obj.dts)
630632
obj.tzinfo = tz
631633
elif not _is_utc(tz):
632-
try:
634+
if (hasattr(tz, 'localize')):
633635
ts = tz.localize(ts)
634-
except AttributeError:
636+
else:
635637
ts = ts.replace(tzinfo=tz)
636-
637638
obj.value = _pydatetime_to_dts(ts, &obj.dts)
638-
offset = _get_utcoffset(ts.tzinfo, ts)
639-
obj.value -= _delta_to_nanoseconds(offset)
640639
obj.tzinfo = ts.tzinfo
641640
else:
642641
# UTC
@@ -645,9 +644,10 @@ cdef convert_to_tsobject(object ts, object tz):
645644
else:
646645
obj.value = _pydatetime_to_dts(ts, &obj.dts)
647646
obj.tzinfo = ts.tzinfo
648-
if obj.tzinfo is not None and not _is_utc(obj.tzinfo):
649-
offset = _get_utcoffset(obj.tzinfo, ts)
650-
obj.value -= _delta_to_nanoseconds(offset)
647+
648+
if obj.tzinfo is not None and not _is_utc(obj.tzinfo):
649+
offset = _get_utcoffset(obj.tzinfo, ts)
650+
obj.value -= _delta_to_nanoseconds(offset)
651651

652652
if is_timestamp(ts):
653653
obj.value += ts.nanosecond

0 commit comments

Comments
 (0)