Skip to content

Commit 848c383

Browse files
committed
BUG: Timestamp doesn't respect tz DST
Add some tests Fix test Add whatsnew Add additional tests and small formatting Adjust test
1 parent 88f5851 commit 848c383

File tree

4 files changed

+29
-6
lines changed

4 files changed

+29
-6
lines changed

doc/source/whatsnew/v0.20.0.txt

+1
Original file line numberDiff line numberDiff line change
@@ -1124,6 +1124,7 @@ Conversion
11241124
- Bug in ``Timestamp.replace`` now raises ``TypeError`` when incorrect argument names are given; previously this raised ``ValueError`` (:issue:`15240`)
11251125
- Bug in ``Timestamp.replace`` with compat for passing long integers (:issue:`15030`)
11261126
- Bug in ``Timestamp`` returning UTC based time/date attributes when a timezone was provided (:issue:`13303`)
1127+
- Bug in ``Timestamp`` incorrectly localizing timezones during construction (:issue:`11481`, :issue:`15777`)
11271128
- Bug in ``TimedeltaIndex`` addition where overflow was being allowed without error (:issue:`14816`)
11281129
- Bug in ``TimedeltaIndex`` raising a ``ValueError`` when boolean indexing with ``loc`` (:issue:`14946`)
11291130
- Bug in catching an overflow in ``Timestamp`` + ``Timedelta/Offset`` operations (:issue:`15126`)

pandas/_libs/tslib.pyx

+3-1
Original file line numberDiff line numberDiff line change
@@ -1569,7 +1569,9 @@ cpdef convert_str_to_tsobject(object ts, object tz, object unit,
15691569
ts = obj.value
15701570
if tz is not None:
15711571
# shift for _localize_tso
1572-
ts = tz_convert_single(ts, tz, 'UTC')
1572+
ts = tz_localize_to_utc(np.array([ts], dtype='i8'), tz,
1573+
ambiguous='raise',
1574+
errors='raise')[0]
15731575
except ValueError:
15741576
try:
15751577
ts = parse_datetime_string(

pandas/tests/series/test_indexing.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -1024,9 +1024,9 @@ def test_setitem_with_tz_dst(self):
10241024
# scalar
10251025
s = orig.copy()
10261026
s[1] = pd.Timestamp('2011-01-01', tz=tz)
1027-
exp = pd.Series([pd.Timestamp('2016-11-06 00:00', tz=tz),
1028-
pd.Timestamp('2011-01-01 00:00', tz=tz),
1029-
pd.Timestamp('2016-11-06 02:00', tz=tz)])
1027+
exp = pd.Series([pd.Timestamp('2016-11-06 00:00-04:00', tz=tz),
1028+
pd.Timestamp('2011-01-01 00:00-05:00', tz=tz),
1029+
pd.Timestamp('2016-11-06 01:00-05:00', tz=tz)])
10301030
tm.assert_series_equal(s, exp)
10311031

10321032
s = orig.copy()

pandas/tests/tseries/test_timezones.py

+22-2
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,26 @@ def test_timestamp_constructed_by_date_and_tz_explicit(self):
159159
self.assertEqual(result.hour, expected.hour)
160160
self.assertEqual(result, expected)
161161

162+
def test_timestamp_constructor_near_dst_boundary(self):
163+
# GH 11481 & 15777
164+
# Naive string timestamps were being localized incorrectly
165+
# with tz_convert_single instead of tz_localize_to_utc
166+
167+
for tz in ['Europe/Brussels', 'Europe/Prague']:
168+
result = Timestamp('2015-10-25 01:00', tz=tz)
169+
expected = Timestamp('2015-10-25 01:00').tz_localize(tz)
170+
self.assertEqual(result, expected)
171+
172+
with tm.assertRaises(pytz.AmbiguousTimeError):
173+
Timestamp('2015-10-25 02:00', tz=tz)
174+
175+
result = Timestamp('2017-03-26 01:00', tz='Europe/Paris')
176+
expected = Timestamp('2017-03-26 01:00').tz_localize('Europe/Paris')
177+
self.assertEqual(result, expected)
178+
179+
with tm.assertRaises(pytz.NonExistentTimeError):
180+
Timestamp('2017-03-26 02:00', tz='Europe/Paris')
181+
162182
def test_timestamp_to_datetime_tzoffset(self):
163183
# tzoffset
164184
from dateutil.tz import tzoffset
@@ -517,8 +537,8 @@ def f():
517537
freq="H"))
518538
if dateutil.__version__ != LooseVersion('2.6.0'):
519539
# GH 14621
520-
self.assertEqual(times[-1], Timestamp('2013-10-27 01:00', tz=tz,
521-
freq="H"))
540+
self.assertEqual(times[-1], Timestamp('2013-10-27 01:00:00+0000',
541+
tz=tz, freq="H"))
522542

523543
def test_ambiguous_nat(self):
524544
tz = self.tz('US/Eastern')

0 commit comments

Comments
 (0)