Skip to content

Commit 89418a3

Browse files
mroeschkejreback
authored andcommitted
TST: Add unit tests for older timezone issues (#21491)
1 parent ea54d39 commit 89418a3

File tree

6 files changed

+61
-1
lines changed

6 files changed

+61
-1
lines changed

doc/source/whatsnew/v0.23.2.txt

+5
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,11 @@ Bug Fixes
8080
**Timezones**
8181
- Bug in :class:`Timestamp` and :class:`DatetimeIndex` where passing a :class:`Timestamp` localized after a DST transition would return a datetime before the DST transition (:issue:`20854`)
8282
- Bug in comparing :class:`DataFrame`s with tz-aware :class:`DatetimeIndex` columns with a DST transition that raised a ``KeyError`` (:issue:`19970`)
83+
- Bug in :meth:`DatetimeIndex.shift` where an ``AssertionError`` would raise when shifting across DST (:issue:`8616`)
84+
- Bug in :class:`Timestamp` constructor where passing an invalid timezone offset designator (``Z``) would not raise a ``ValueError``(:issue:`8910`)
85+
- Bug in :meth:`Timestamp.replace` where replacing at a DST boundary would retain an incorrect offset (:issue:`7825`)
86+
- Bug in :meth:`DatetimeIndex.reindex` when reindexing a tz-naive and tz-aware :class:`DatetimeIndex` (:issue:`8306`)
87+
- Bug in :meth:`DatetimeIndex.resample` when downsampling across a DST boundary (:issue:`8531`)
8388

8489
**Other**
8590

pandas/tests/indexes/datetimes/test_arithmetic.py

+23-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
import operator
55

66
import pytest
7-
7+
import pytz
88
import numpy as np
99

1010
import pandas as pd
@@ -476,6 +476,28 @@ def test_dti_shift_localized(self, tzstr):
476476
result = dr_tz.shift(1, '10T')
477477
assert result.tz == dr_tz.tz
478478

479+
def test_dti_shift_across_dst(self):
480+
# GH 8616
481+
idx = date_range('2013-11-03', tz='America/Chicago',
482+
periods=7, freq='H')
483+
s = Series(index=idx[:-1])
484+
result = s.shift(freq='H')
485+
expected = Series(index=idx[1:])
486+
tm.assert_series_equal(result, expected)
487+
488+
@pytest.mark.parametrize('shift, result_time', [
489+
[0, '2014-11-14 00:00:00'],
490+
[-1, '2014-11-13 23:00:00'],
491+
[1, '2014-11-14 01:00:00']])
492+
def test_dti_shift_near_midnight(self, shift, result_time):
493+
# GH 8616
494+
dt = datetime(2014, 11, 14, 0)
495+
dt_est = pytz.timezone('EST').localize(dt)
496+
s = Series(data=[1], index=[dt_est])
497+
result = s.shift(shift, freq='H')
498+
expected = Series(1, index=DatetimeIndex([result_time], tz='EST'))
499+
tm.assert_series_equal(result, expected)
500+
479501
# -------------------------------------------------------------
480502
# Binary operations DatetimeIndex and timedelta-like
481503

pandas/tests/scalar/timestamp/test_timestamp.py

+6
Original file line numberDiff line numberDiff line change
@@ -420,6 +420,12 @@ def test_constructor_nanosecond(self, result):
420420
expected = expected + Timedelta(nanoseconds=1)
421421
assert result == expected
422422

423+
@pytest.mark.parametrize('z', ['Z0', 'Z00'])
424+
def test_constructor_invalid_Z0_isostring(self, z):
425+
# GH 8910
426+
with pytest.raises(ValueError):
427+
Timestamp('2014-11-02 01:00{}'.format(z))
428+
423429
@pytest.mark.parametrize('arg', ['year', 'month', 'day', 'hour', 'minute',
424430
'second', 'microsecond', 'nanosecond'])
425431
def test_invalid_date_kwarg_with_string_input(self, arg):

pandas/tests/scalar/timestamp/test_unary_ops.py

+7
Original file line numberDiff line numberDiff line change
@@ -238,6 +238,13 @@ def test_replace_across_dst(self, tz, normalize):
238238
ts2b = normalize(ts2)
239239
assert ts2 == ts2b
240240

241+
def test_replace_dst_border(self):
242+
# Gh 7825
243+
t = Timestamp('2013-11-3', tz='America/Chicago')
244+
result = t.replace(hour=3)
245+
expected = Timestamp('2013-11-3 03:00:00', tz='America/Chicago')
246+
assert result == expected
247+
241248
# --------------------------------------------------------------
242249

243250
@td.skip_if_windows

pandas/tests/series/indexing/test_alter_index.py

+9
Original file line numberDiff line numberDiff line change
@@ -453,6 +453,15 @@ def test_reindex_fill_value():
453453
assert_series_equal(result, expected)
454454

455455

456+
def test_reindex_datetimeindexes_tz_naive_and_aware():
457+
# GH 8306
458+
idx = date_range('20131101', tz='America/Chicago', periods=7)
459+
newidx = date_range('20131103', periods=10, freq='H')
460+
s = Series(range(7), index=idx)
461+
with pytest.raises(TypeError):
462+
s.reindex(newidx, method='ffill')
463+
464+
456465
def test_rename():
457466
# GH 17407
458467
s = Series(range(1, 6), index=pd.Index(range(2, 7), name='IntIndex'))

pandas/tests/test_resample.py

+11
Original file line numberDiff line numberDiff line change
@@ -2084,6 +2084,17 @@ def test_resample_dst_anchor(self):
20842084
freq='D', tz='Europe/Paris')),
20852085
'D Frequency')
20862086

2087+
def test_downsample_across_dst(self):
2088+
# GH 8531
2089+
tz = pytz.timezone('Europe/Berlin')
2090+
dt = datetime(2014, 10, 26)
2091+
dates = date_range(tz.localize(dt), periods=4, freq='2H')
2092+
result = Series(5, index=dates).resample('H').mean()
2093+
expected = Series([5., np.nan] * 3 + [5.],
2094+
index=date_range(tz.localize(dt), periods=7,
2095+
freq='H'))
2096+
tm.assert_series_equal(result, expected)
2097+
20872098
def test_resample_with_nat(self):
20882099
# GH 13020
20892100
index = DatetimeIndex([pd.NaT,

0 commit comments

Comments
 (0)