Skip to content

TST: dateutil fixes (GH8639) #9047

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Dec 10, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions doc/source/whatsnew/v0.15.2.txt
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,7 @@ Bug Fixes
- Bug in ``DatetimeIndex`` when using ``time`` object as key (:issue:`8667`)
- Bug in ``merge`` where ``how='left'`` and ``sort=False`` would not preserve left frame order (:issue:`7331`)
- Bug in ``MultiIndex.reindex`` where reindexing at level would not reorder labels (:issue:`4088`)
- Bug in certain operations with dateutil timezones, manifesting with dateutil 2.3 (:issue:`8639`)

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

Expand Down
18 changes: 9 additions & 9 deletions pandas/tseries/tests/test_daterange.py
Original file line number Diff line number Diff line change
Expand Up @@ -371,31 +371,31 @@ def test_range_tz_pytz(self):
self.assertEqual(dr.tz.zone, tz.zone)
self.assertEqual(dr[0], start)
self.assertEqual(dr[2], end)

def test_range_tz_dst_straddle_pytz(self):

tm._skip_if_no_pytz()
from pytz import timezone
tz = timezone('US/Eastern')
dates = [(tz.localize(datetime(2014, 3, 6)),
dates = [(tz.localize(datetime(2014, 3, 6)),
tz.localize(datetime(2014, 3, 12))),
(tz.localize(datetime(2013, 11, 1)),
(tz.localize(datetime(2013, 11, 1)),
tz.localize(datetime(2013, 11, 6)))]
for (start, end) in dates:
dr = date_range(start, end, freq='D')
self.assertEqual(dr[0], start)
self.assertEqual(dr[-1], end)
self.assertEqual(np.all(dr.hour==0), True)

dr = date_range(start, end, freq='D', tz='US/Eastern')
self.assertEqual(dr[0], start)
self.assertEqual(dr[-1], end)
self.assertEqual(np.all(dr.hour==0), True)
self.assertEqual(np.all(dr.hour==0), True)

dr = date_range(start.replace(tzinfo=None), end.replace(tzinfo=None), freq='D', tz='US/Eastern')
self.assertEqual(dr[0], start)
self.assertEqual(dr[-1], end)
self.assertEqual(np.all(dr.hour==0), True)
self.assertEqual(np.all(dr.hour==0), True)

def test_range_tz_dateutil(self):
# GH 2906
Expand Down Expand Up @@ -441,7 +441,7 @@ def test_month_range_union_tz_pytz(self):
def test_month_range_union_tz_dateutil(self):
_skip_if_windows_python_3()
tm._skip_if_no_dateutil()
from dateutil.tz import gettz as timezone
from dateutil.zoneinfo import gettz as timezone
tz = timezone('US/Eastern')

early_start = datetime(2011, 1, 1)
Expand Down
4 changes: 2 additions & 2 deletions pandas/tseries/tests/test_timeseries.py
Original file line number Diff line number Diff line change
Expand Up @@ -419,7 +419,7 @@ def test_timestamp_to_datetime_explicit_dateutil(self):
tm._skip_if_no_dateutil()
import dateutil
rng = date_range('20090415', '20090519',
tz=dateutil.tz.gettz('US/Eastern'))
tz=dateutil.zoneinfo.gettz('US/Eastern'))

stamp = rng[0]
dtval = stamp.to_pydatetime()
Expand Down Expand Up @@ -1797,7 +1797,7 @@ def test_append_concat_tz_explicit_pytz(self):
def test_append_concat_tz_dateutil(self):
# GH 2938
tm._skip_if_no_dateutil()
from dateutil.tz import gettz as timezone
from dateutil.zoneinfo import gettz as timezone

rng = date_range('5/8/2012 1:45', periods=10, freq='5T',
tz='dateutil/US/Eastern')
Expand Down
20 changes: 10 additions & 10 deletions pandas/tseries/tests/test_timezones.py
Original file line number Diff line number Diff line change
Expand Up @@ -443,7 +443,7 @@ def test_ambiguous_infer(self):
localized_old = di.tz_localize(tz, infer_dst=True)
self.assert_numpy_array_equal(dr, localized_old)
self.assert_numpy_array_equal(dr, DatetimeIndex(times, tz=tz, ambiguous='infer'))

# When there is no dst transition, nothing special happens
dr = date_range(datetime(2011, 6, 1, 0), periods=10,
freq=datetools.Hour())
Expand All @@ -463,31 +463,31 @@ def test_ambiguous_flags(self):
times = ['11/06/2011 00:00', '11/06/2011 01:00',
'11/06/2011 01:00', '11/06/2011 02:00',
'11/06/2011 03:00']

# Test tz_localize
di = DatetimeIndex(times)
is_dst = [1, 1, 0, 0, 0]
localized = di.tz_localize(tz, ambiguous=is_dst)
self.assert_numpy_array_equal(dr, localized)
self.assert_numpy_array_equal(dr, DatetimeIndex(times, tz=tz, ambiguous=is_dst))

localized = di.tz_localize(tz, ambiguous=np.array(is_dst))
self.assert_numpy_array_equal(dr, localized)

localized = di.tz_localize(tz, ambiguous=np.array(is_dst).astype('bool'))
self.assert_numpy_array_equal(dr, localized)

# Test constructor
localized = DatetimeIndex(times, tz=tz, ambiguous=is_dst)
self.assert_numpy_array_equal(dr, localized)

# Test duplicate times where infer_dst fails
times += times
di = DatetimeIndex(times)

# When the sizes are incompatible, make sure error is raised
self.assertRaises(Exception, di.tz_localize, tz, ambiguous=is_dst)

# When sizes are compatible and there are repeats ('infer' won't work)
is_dst = np.hstack((is_dst, is_dst))
localized = di.tz_localize(tz, ambiguous=is_dst)
Expand All @@ -501,15 +501,15 @@ def test_ambiguous_flags(self):
localized = dr.tz_localize(tz)
localized_is_dst = dr.tz_localize(tz, ambiguous=is_dst)
self.assert_numpy_array_equal(localized, localized_is_dst)

def test_ambiguous_nat(self):
tz = self.tz('US/Eastern')
times = ['11/06/2011 00:00', '11/06/2011 01:00',
'11/06/2011 01:00', '11/06/2011 02:00',
'11/06/2011 03:00']
di = DatetimeIndex(times)
localized = di.tz_localize(tz, ambiguous='NaT')

times = ['11/06/2011 00:00', np.NaN,
np.NaN, '11/06/2011 02:00',
'11/06/2011 03:00']
Expand Down
25 changes: 18 additions & 7 deletions pandas/tseries/tests/test_tslib.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import nose

from distutils.version import LooseVersion
import numpy as np

from pandas import tslib
Expand Down Expand Up @@ -137,13 +137,24 @@ def test_constructor_with_stringoffset(self):
self.assertEqual(result, eval(repr(result)))

def test_repr(self):
tm._skip_if_no_pytz()
tm._skip_if_no_dateutil()

dates = ['2014-03-07', '2014-01-01 09:00', '2014-01-01 00:00:00.000000001']
timezones = ['UTC', 'Asia/Tokyo', 'US/Eastern', 'dateutil/America/Los_Angeles']

# dateutil zone change (only matters for repr)
import dateutil
if dateutil.__version__ >= LooseVersion('2.3'):
timezones = ['UTC', 'Asia/Tokyo', 'US/Eastern', 'dateutil/US/Pacific']
else:
timezones = ['UTC', 'Asia/Tokyo', 'US/Eastern', 'dateutil/America/Los_Angeles']

freqs = ['D', 'M', 'S', 'N']

for date in dates:
for tz in timezones:
for freq in freqs:

# avoid to match with timezone name
freq_repr = "'{0}'".format(freq)
if tz.startswith('dateutil'):
Expand Down Expand Up @@ -306,10 +317,10 @@ def test_now(self):
ts_from_string = Timestamp('now')
ts_from_method = Timestamp.now()
ts_datetime = datetime.datetime.now()

ts_from_string_tz = Timestamp('now', tz='US/Eastern')
ts_from_method_tz = Timestamp.now(tz='US/Eastern')

# Check that the delta between the times is less than 1s (arbitrarily small)
delta = Timedelta(seconds=1)
self.assertTrue((ts_from_method - ts_from_string) < delta)
Expand All @@ -321,10 +332,10 @@ def test_today(self):
ts_from_string = Timestamp('today')
ts_from_method = Timestamp.today()
ts_datetime = datetime.datetime.today()

ts_from_string_tz = Timestamp('today', tz='US/Eastern')
ts_from_method_tz = Timestamp.today(tz='US/Eastern')

# Check that the delta between the times is less than 1s (arbitrarily small)
delta = Timedelta(seconds=1)
self.assertTrue((ts_from_method - ts_from_string) < delta)
Expand Down Expand Up @@ -737,7 +748,7 @@ def test_resolution(self):
for freq, expected in zip(['A', 'Q', 'M', 'D', 'H', 'T', 'S', 'L', 'U'],
[tslib.D_RESO, tslib.D_RESO, tslib.D_RESO, tslib.D_RESO,
tslib.H_RESO, tslib.T_RESO,tslib.S_RESO, tslib.MS_RESO, tslib.US_RESO]):
for tz in [None, 'Asia/Tokyo', 'US/Eastern']:
for tz in [None, 'Asia/Tokyo', 'US/Eastern', 'dateutil/US/Eastern']:
idx = date_range(start='2013-04-01', periods=30, freq=freq, tz=tz)
result = tslib.resolution(idx.asi8, idx.tz)
self.assertEqual(result, expected)
Expand Down
Loading