Skip to content

Commit 9b9c10a

Browse files
committed
TST: dateutil fixes (GH8639)
1 parent 4f526fe commit 9b9c10a

File tree

6 files changed

+114
-107
lines changed

6 files changed

+114
-107
lines changed

doc/source/whatsnew/v0.15.2.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,7 @@ Bug Fixes
163163
- Bug in ``DatetimeIndex`` when using ``time`` object as key (:issue:`8667`)
164164
- Bug in ``merge`` where ``how='left'`` and ``sort=False`` would not preserve left frame order (:issue:`7331`)
165165
- Bug in ``MultiIndex.reindex`` where reindexing at level would not reorder labels (:issue:`4088`)
166+
- Bug in certain operations with dateutil timezones, manifesting with dateutil 2.3 (:issue:`8639`)
166167

167168
- Fix negative step support for label-based slices (:issue:`8753`)
168169

pandas/tseries/tests/test_daterange.py

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -371,31 +371,31 @@ def test_range_tz_pytz(self):
371371
self.assertEqual(dr.tz.zone, tz.zone)
372372
self.assertEqual(dr[0], start)
373373
self.assertEqual(dr[2], end)
374-
374+
375375
def test_range_tz_dst_straddle_pytz(self):
376-
376+
377377
tm._skip_if_no_pytz()
378378
from pytz import timezone
379379
tz = timezone('US/Eastern')
380-
dates = [(tz.localize(datetime(2014, 3, 6)),
380+
dates = [(tz.localize(datetime(2014, 3, 6)),
381381
tz.localize(datetime(2014, 3, 12))),
382-
(tz.localize(datetime(2013, 11, 1)),
382+
(tz.localize(datetime(2013, 11, 1)),
383383
tz.localize(datetime(2013, 11, 6)))]
384384
for (start, end) in dates:
385385
dr = date_range(start, end, freq='D')
386386
self.assertEqual(dr[0], start)
387387
self.assertEqual(dr[-1], end)
388388
self.assertEqual(np.all(dr.hour==0), True)
389-
389+
390390
dr = date_range(start, end, freq='D', tz='US/Eastern')
391391
self.assertEqual(dr[0], start)
392392
self.assertEqual(dr[-1], end)
393-
self.assertEqual(np.all(dr.hour==0), True)
394-
393+
self.assertEqual(np.all(dr.hour==0), True)
394+
395395
dr = date_range(start.replace(tzinfo=None), end.replace(tzinfo=None), freq='D', tz='US/Eastern')
396396
self.assertEqual(dr[0], start)
397397
self.assertEqual(dr[-1], end)
398-
self.assertEqual(np.all(dr.hour==0), True)
398+
self.assertEqual(np.all(dr.hour==0), True)
399399

400400
def test_range_tz_dateutil(self):
401401
# GH 2906
@@ -441,7 +441,7 @@ def test_month_range_union_tz_pytz(self):
441441
def test_month_range_union_tz_dateutil(self):
442442
_skip_if_windows_python_3()
443443
tm._skip_if_no_dateutil()
444-
from dateutil.tz import gettz as timezone
444+
from dateutil.zoneinfo import gettz as timezone
445445
tz = timezone('US/Eastern')
446446

447447
early_start = datetime(2011, 1, 1)

pandas/tseries/tests/test_timeseries.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -419,7 +419,7 @@ def test_timestamp_to_datetime_explicit_dateutil(self):
419419
tm._skip_if_no_dateutil()
420420
import dateutil
421421
rng = date_range('20090415', '20090519',
422-
tz=dateutil.tz.gettz('US/Eastern'))
422+
tz=dateutil.zoneinfo.gettz('US/Eastern'))
423423

424424
stamp = rng[0]
425425
dtval = stamp.to_pydatetime()
@@ -1797,7 +1797,7 @@ def test_append_concat_tz_explicit_pytz(self):
17971797
def test_append_concat_tz_dateutil(self):
17981798
# GH 2938
17991799
tm._skip_if_no_dateutil()
1800-
from dateutil.tz import gettz as timezone
1800+
from dateutil.zoneinfo import gettz as timezone
18011801

18021802
rng = date_range('5/8/2012 1:45', periods=10, freq='5T',
18031803
tz='dateutil/US/Eastern')

pandas/tseries/tests/test_timezones.py

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -443,7 +443,7 @@ def test_ambiguous_infer(self):
443443
localized_old = di.tz_localize(tz, infer_dst=True)
444444
self.assert_numpy_array_equal(dr, localized_old)
445445
self.assert_numpy_array_equal(dr, DatetimeIndex(times, tz=tz, ambiguous='infer'))
446-
446+
447447
# When there is no dst transition, nothing special happens
448448
dr = date_range(datetime(2011, 6, 1, 0), periods=10,
449449
freq=datetools.Hour())
@@ -463,31 +463,31 @@ def test_ambiguous_flags(self):
463463
times = ['11/06/2011 00:00', '11/06/2011 01:00',
464464
'11/06/2011 01:00', '11/06/2011 02:00',
465465
'11/06/2011 03:00']
466-
466+
467467
# Test tz_localize
468468
di = DatetimeIndex(times)
469469
is_dst = [1, 1, 0, 0, 0]
470470
localized = di.tz_localize(tz, ambiguous=is_dst)
471471
self.assert_numpy_array_equal(dr, localized)
472472
self.assert_numpy_array_equal(dr, DatetimeIndex(times, tz=tz, ambiguous=is_dst))
473-
473+
474474
localized = di.tz_localize(tz, ambiguous=np.array(is_dst))
475475
self.assert_numpy_array_equal(dr, localized)
476-
476+
477477
localized = di.tz_localize(tz, ambiguous=np.array(is_dst).astype('bool'))
478478
self.assert_numpy_array_equal(dr, localized)
479-
479+
480480
# Test constructor
481481
localized = DatetimeIndex(times, tz=tz, ambiguous=is_dst)
482482
self.assert_numpy_array_equal(dr, localized)
483-
483+
484484
# Test duplicate times where infer_dst fails
485485
times += times
486486
di = DatetimeIndex(times)
487-
487+
488488
# When the sizes are incompatible, make sure error is raised
489489
self.assertRaises(Exception, di.tz_localize, tz, ambiguous=is_dst)
490-
490+
491491
# When sizes are compatible and there are repeats ('infer' won't work)
492492
is_dst = np.hstack((is_dst, is_dst))
493493
localized = di.tz_localize(tz, ambiguous=is_dst)
@@ -501,15 +501,15 @@ def test_ambiguous_flags(self):
501501
localized = dr.tz_localize(tz)
502502
localized_is_dst = dr.tz_localize(tz, ambiguous=is_dst)
503503
self.assert_numpy_array_equal(localized, localized_is_dst)
504-
504+
505505
def test_ambiguous_nat(self):
506506
tz = self.tz('US/Eastern')
507507
times = ['11/06/2011 00:00', '11/06/2011 01:00',
508508
'11/06/2011 01:00', '11/06/2011 02:00',
509509
'11/06/2011 03:00']
510510
di = DatetimeIndex(times)
511511
localized = di.tz_localize(tz, ambiguous='NaT')
512-
512+
513513
times = ['11/06/2011 00:00', np.NaN,
514514
np.NaN, '11/06/2011 02:00',
515515
'11/06/2011 03:00']

pandas/tseries/tests/test_tslib.py

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import nose
2-
2+
from distutils.version import LooseVersion
33
import numpy as np
44

55
from pandas import tslib
@@ -137,13 +137,24 @@ def test_constructor_with_stringoffset(self):
137137
self.assertEqual(result, eval(repr(result)))
138138

139139
def test_repr(self):
140+
tm._skip_if_no_pytz()
141+
tm._skip_if_no_dateutil()
142+
140143
dates = ['2014-03-07', '2014-01-01 09:00', '2014-01-01 00:00:00.000000001']
141-
timezones = ['UTC', 'Asia/Tokyo', 'US/Eastern', 'dateutil/America/Los_Angeles']
144+
145+
# dateutil zone change (only matters for repr)
146+
import dateutil
147+
if dateutil.__version__ >= LooseVersion('2.3'):
148+
timezones = ['UTC', 'Asia/Tokyo', 'US/Eastern', 'dateutil/US/Pacific']
149+
else:
150+
timezones = ['UTC', 'Asia/Tokyo', 'US/Eastern', 'dateutil/America/Los_Angeles']
151+
142152
freqs = ['D', 'M', 'S', 'N']
143153

144154
for date in dates:
145155
for tz in timezones:
146156
for freq in freqs:
157+
147158
# avoid to match with timezone name
148159
freq_repr = "'{0}'".format(freq)
149160
if tz.startswith('dateutil'):
@@ -306,10 +317,10 @@ def test_now(self):
306317
ts_from_string = Timestamp('now')
307318
ts_from_method = Timestamp.now()
308319
ts_datetime = datetime.datetime.now()
309-
320+
310321
ts_from_string_tz = Timestamp('now', tz='US/Eastern')
311322
ts_from_method_tz = Timestamp.now(tz='US/Eastern')
312-
323+
313324
# Check that the delta between the times is less than 1s (arbitrarily small)
314325
delta = Timedelta(seconds=1)
315326
self.assertTrue((ts_from_method - ts_from_string) < delta)
@@ -321,10 +332,10 @@ def test_today(self):
321332
ts_from_string = Timestamp('today')
322333
ts_from_method = Timestamp.today()
323334
ts_datetime = datetime.datetime.today()
324-
335+
325336
ts_from_string_tz = Timestamp('today', tz='US/Eastern')
326337
ts_from_method_tz = Timestamp.today(tz='US/Eastern')
327-
338+
328339
# Check that the delta between the times is less than 1s (arbitrarily small)
329340
delta = Timedelta(seconds=1)
330341
self.assertTrue((ts_from_method - ts_from_string) < delta)
@@ -737,7 +748,7 @@ def test_resolution(self):
737748
for freq, expected in zip(['A', 'Q', 'M', 'D', 'H', 'T', 'S', 'L', 'U'],
738749
[tslib.D_RESO, tslib.D_RESO, tslib.D_RESO, tslib.D_RESO,
739750
tslib.H_RESO, tslib.T_RESO,tslib.S_RESO, tslib.MS_RESO, tslib.US_RESO]):
740-
for tz in [None, 'Asia/Tokyo', 'US/Eastern']:
751+
for tz in [None, 'Asia/Tokyo', 'US/Eastern', 'dateutil/US/Eastern']:
741752
idx = date_range(start='2013-04-01', periods=30, freq=freq, tz=tz)
742753
result = tslib.resolution(idx.asi8, idx.tz)
743754
self.assertEqual(result, expected)

0 commit comments

Comments
 (0)