Skip to content

Commit

Permalink
BUG: Timestamp doesn't respect tz DST
Browse files Browse the repository at this point in the history
closes #11481
closes #15777
  • Loading branch information
mroeschke authored and jreback committed Apr 8, 2017
1 parent 88f5851 commit 44ff21d
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 6 deletions.
1 change: 1 addition & 0 deletions doc/source/whatsnew/v0.20.0.txt
Original file line number Diff line number Diff line change
Expand Up @@ -1124,6 +1124,7 @@ Conversion
- Bug in ``Timestamp.replace`` now raises ``TypeError`` when incorrect argument names are given; previously this raised ``ValueError`` (:issue:`15240`)
- Bug in ``Timestamp.replace`` with compat for passing long integers (:issue:`15030`)
- Bug in ``Timestamp`` returning UTC based time/date attributes when a timezone was provided (:issue:`13303`)
- Bug in ``Timestamp`` incorrectly localizing timezones during construction (:issue:`11481`, :issue:`15777`)
- Bug in ``TimedeltaIndex`` addition where overflow was being allowed without error (:issue:`14816`)
- Bug in ``TimedeltaIndex`` raising a ``ValueError`` when boolean indexing with ``loc`` (:issue:`14946`)
- Bug in catching an overflow in ``Timestamp`` + ``Timedelta/Offset`` operations (:issue:`15126`)
Expand Down
4 changes: 3 additions & 1 deletion pandas/_libs/tslib.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -1569,7 +1569,9 @@ cpdef convert_str_to_tsobject(object ts, object tz, object unit,
ts = obj.value
if tz is not None:
# shift for _localize_tso
ts = tz_convert_single(ts, tz, 'UTC')
ts = tz_localize_to_utc(np.array([ts], dtype='i8'), tz,
ambiguous='raise',
errors='raise')[0]
except ValueError:
try:
ts = parse_datetime_string(
Expand Down
6 changes: 3 additions & 3 deletions pandas/tests/series/test_indexing.py
Original file line number Diff line number Diff line change
Expand Up @@ -1024,9 +1024,9 @@ def test_setitem_with_tz_dst(self):
# scalar
s = orig.copy()
s[1] = pd.Timestamp('2011-01-01', tz=tz)
exp = pd.Series([pd.Timestamp('2016-11-06 00:00', tz=tz),
pd.Timestamp('2011-01-01 00:00', tz=tz),
pd.Timestamp('2016-11-06 02:00', tz=tz)])
exp = pd.Series([pd.Timestamp('2016-11-06 00:00-04:00', tz=tz),
pd.Timestamp('2011-01-01 00:00-05:00', tz=tz),
pd.Timestamp('2016-11-06 01:00-05:00', tz=tz)])
tm.assert_series_equal(s, exp)

s = orig.copy()
Expand Down
24 changes: 22 additions & 2 deletions pandas/tests/tseries/test_timezones.py
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,26 @@ def test_timestamp_constructed_by_date_and_tz_explicit(self):
self.assertEqual(result.hour, expected.hour)
self.assertEqual(result, expected)

def test_timestamp_constructor_near_dst_boundary(self):
# GH 11481 & 15777
# Naive string timestamps were being localized incorrectly
# with tz_convert_single instead of tz_localize_to_utc

for tz in ['Europe/Brussels', 'Europe/Prague']:
result = Timestamp('2015-10-25 01:00', tz=tz)
expected = Timestamp('2015-10-25 01:00').tz_localize(tz)
self.assertEqual(result, expected)

with tm.assertRaises(pytz.AmbiguousTimeError):
Timestamp('2015-10-25 02:00', tz=tz)

result = Timestamp('2017-03-26 01:00', tz='Europe/Paris')
expected = Timestamp('2017-03-26 01:00').tz_localize('Europe/Paris')
self.assertEqual(result, expected)

with tm.assertRaises(pytz.NonExistentTimeError):
Timestamp('2017-03-26 02:00', tz='Europe/Paris')

def test_timestamp_to_datetime_tzoffset(self):
# tzoffset
from dateutil.tz import tzoffset
Expand Down Expand Up @@ -517,8 +537,8 @@ def f():
freq="H"))
if dateutil.__version__ != LooseVersion('2.6.0'):
# GH 14621
self.assertEqual(times[-1], Timestamp('2013-10-27 01:00', tz=tz,
freq="H"))
self.assertEqual(times[-1], Timestamp('2013-10-27 01:00:00+0000',
tz=tz, freq="H"))

def test_ambiguous_nat(self):
tz = self.tz('US/Eastern')
Expand Down

0 comments on commit 44ff21d

Please sign in to comment.