From 213b0659b0589ea357b790c565dbdca20d9fb8c9 Mon Sep 17 00:00:00 2001 From: aschade Date: Sat, 25 Nov 2017 11:37:04 -0500 Subject: [PATCH 1/8] DOC: Add whatsnew note --- doc/source/whatsnew/v0.21.1.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/doc/source/whatsnew/v0.21.1.txt b/doc/source/whatsnew/v0.21.1.txt index 637ccf0603e0f..db1f685cb16f7 100644 --- a/doc/source/whatsnew/v0.21.1.txt +++ b/doc/source/whatsnew/v0.21.1.txt @@ -64,7 +64,7 @@ Conversion - Bug in :class:`DatetimeIndex` subtracting datetimelike from DatetimeIndex could fail to overflow (:issue:`18020`) - Bug in :meth:`IntervalIndex.copy` when copying and ``IntervalIndex`` with non-default ``closed`` (:issue:`18339`) - Bug in :func:`DataFrame.to_dict` where columns of datetime that are tz-aware were not converted to required arrays when used with ``orient='records'``, raising``TypeError` (:issue:`18372`) -- +- Bug in :class:`DateTimeIndex` and :meth:`date_range` where mismatching tz-aware ``start`` and ``end`` timezones would not raise an err if ``end.tzinfo`` is None (:issue:`18431`) - Indexing From 1e38da4904b3ae21f0c39c39910b891146bbf608 Mon Sep 17 00:00:00 2001 From: aschade Date: Sat, 25 Nov 2017 11:38:06 -0500 Subject: [PATCH 2/8] TST: Added test for date_range and DateTimeIndex with mismatching timezones --- .../tests/indexes/datetimes/test_date_range.py | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/pandas/tests/indexes/datetimes/test_date_range.py b/pandas/tests/indexes/datetimes/test_date_range.py index edcee0479827f..d6e7e6162ad3b 100644 --- a/pandas/tests/indexes/datetimes/test_date_range.py +++ b/pandas/tests/indexes/datetimes/test_date_range.py @@ -290,6 +290,24 @@ def test_precision_finer_than_offset(self): tm.assert_index_equal(result1, expected1) tm.assert_index_equal(result2, expected2) + def test_mismatching_tz_raises_err(self): + dt1_tz = pd.Timestamp('2017-01-01', tz='US/Eastern') + dt1_no_tz = pd.Timestamp('2017-01-01') + dt2_tz = pd.Timestamp('2017-01-04', tz='US/Eastern') + dt2_no_tz = pd.Timestamp('2017-01-04') + + with pytest.raises(TypeError): + pd.date_range(start=dt1_no_tz, end=dt2_tz) + + with pytest.raises(TypeError): + pd.date_range(start=dt1_tz, end=dt2_no_tz) + + with pytest.raises(TypeError): + pd.DatetimeIndex(start=dt1_no_tz, end=dt2_tz, freq='D') + + with pytest.raises(TypeError): + pd.DatetimeIndex(start=dt1_tz, end=dt2_no_tz, freq='D') + class TestBusinessDateRange(object): From 68652397d1d6624e425ea14f3917f97af9e3782a Mon Sep 17 00:00:00 2001 From: aschade Date: Sat, 25 Nov 2017 11:39:07 -0500 Subject: [PATCH 3/8] BUG: Fixed exception not being raised if end.tzinfo is None but start.tzinfo is not None --- pandas/_libs/tslibs/timezones.pyx | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/pandas/_libs/tslibs/timezones.pyx b/pandas/_libs/tslibs/timezones.pyx index 7fb48e7c66f47..d326f2cb68f24 100644 --- a/pandas/_libs/tslibs/timezones.pyx +++ b/pandas/_libs/tslibs/timezones.pyx @@ -284,10 +284,9 @@ cdef object get_dst_info(object tz): def infer_tzinfo(start, end): if start is not None and end is not None: tz = start.tzinfo - if end.tzinfo: - if not (get_timezone(tz) == get_timezone(end.tzinfo)): - msg = 'Inputs must both have the same timezone, {tz1} != {tz2}' - raise AssertionError(msg.format(tz1=tz, tz2=end.tzinfo)) + if not (get_timezone(tz) == get_timezone(end.tzinfo)): + msg = 'Inputs must both have the same timezone, {tz1} != {tz2}' + raise AssertionError(msg.format(tz1=tz, tz2=end.tzinfo)) elif start is not None: tz = start.tzinfo elif end is not None: From 925fedecd9fa318fa272c49a7b66a3b864ae17b1 Mon Sep 17 00:00:00 2001 From: aschade Date: Sat, 25 Nov 2017 13:53:08 -0500 Subject: [PATCH 4/8] TST: Fixed new test failure in test_with_tz to pass in a tzaware end date --- pandas/tests/tseries/test_timezones.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pandas/tests/tseries/test_timezones.py b/pandas/tests/tseries/test_timezones.py index 3dfad2d4af75e..a01166daf6be1 100644 --- a/pandas/tests/tseries/test_timezones.py +++ b/pandas/tests/tseries/test_timezones.py @@ -424,7 +424,7 @@ def test_with_tz(self): # datetimes with tzinfo set dr = bdate_range(datetime(2005, 1, 1, tzinfo=pytz.utc), - '1/1/2009', tz=pytz.utc) + datetime(2009, 1, 1, tzinfo=pytz.utc)) pytest.raises(Exception, bdate_range, datetime(2005, 1, 1, tzinfo=pytz.utc), '1/1/2009', From 58d5bea51f011425d6793cd6791ed8047c6dfffd Mon Sep 17 00:00:00 2001 From: aschade Date: Sat, 25 Nov 2017 18:55:22 -0500 Subject: [PATCH 5/8] TST: Updated test to include diff tz and parametrized a method for the exception testing --- .../indexes/datetimes/test_date_range.py | 27 ++++++++++--------- 1 file changed, 14 insertions(+), 13 deletions(-) diff --git a/pandas/tests/indexes/datetimes/test_date_range.py b/pandas/tests/indexes/datetimes/test_date_range.py index d6e7e6162ad3b..a0448a688bdde 100644 --- a/pandas/tests/indexes/datetimes/test_date_range.py +++ b/pandas/tests/indexes/datetimes/test_date_range.py @@ -291,23 +291,24 @@ def test_precision_finer_than_offset(self): tm.assert_index_equal(result2, expected2) def test_mismatching_tz_raises_err(self): + #issue 18488 dt1_tz = pd.Timestamp('2017-01-01', tz='US/Eastern') dt1_no_tz = pd.Timestamp('2017-01-01') dt2_tz = pd.Timestamp('2017-01-04', tz='US/Eastern') dt2_no_tz = pd.Timestamp('2017-01-04') - - with pytest.raises(TypeError): - pd.date_range(start=dt1_no_tz, end=dt2_tz) - - with pytest.raises(TypeError): - pd.date_range(start=dt1_tz, end=dt2_no_tz) - - with pytest.raises(TypeError): - pd.DatetimeIndex(start=dt1_no_tz, end=dt2_tz, freq='D') - - with pytest.raises(TypeError): - pd.DatetimeIndex(start=dt1_tz, end=dt2_no_tz, freq='D') - + dt1_diff_tz = pd.Timestamp('2017-01-01', tz='Europe/London') + dt2_diff_tz = pd.Timestamp('2017-01-04', tz='Europe/London') + + def assert_tz_raises_exception(start, end): + with pytest.raises(TypeError): + pd.date_range(start, end) + with pytest.raises(TypeError): + pd.DatetimeIndexstart(start, end) + + assert_tz_raises_exception(dt1_no_tz, dt2_tz) + assert_tz_raises_exception(dt1_tz, dt2_no_tz) + assert_tz_raises_exception(dt1_tz, dt2_diff_tz) + assert_tz_raises_exception(dt1_diff_tz, dt2_tz) class TestBusinessDateRange(object): From 3a7ec72109c39a97ce57ebebe45ffb1d271654c8 Mon Sep 17 00:00:00 2001 From: aschade Date: Sat, 25 Nov 2017 20:22:50 -0500 Subject: [PATCH 6/8] TST: Using parametrize for the test --- .../indexes/datetimes/test_date_range.py | 31 ++++++++----------- 1 file changed, 13 insertions(+), 18 deletions(-) diff --git a/pandas/tests/indexes/datetimes/test_date_range.py b/pandas/tests/indexes/datetimes/test_date_range.py index a0448a688bdde..629a6681a9984 100644 --- a/pandas/tests/indexes/datetimes/test_date_range.py +++ b/pandas/tests/indexes/datetimes/test_date_range.py @@ -290,25 +290,20 @@ def test_precision_finer_than_offset(self): tm.assert_index_equal(result1, expected1) tm.assert_index_equal(result2, expected2) - def test_mismatching_tz_raises_err(self): + dt1, dt2, tz1, tz2 = '2017-01-01', '2017-01-01', 'US/Eastern', 'Europe/London' + @pytest.mark.parametrize("start,end", [ + (pd.Timestamp(dt1, tz=tz1), pd.Timestamp(dt2)), + (pd.Timestamp(dt1), pd.Timestamp(dt2, tz=tz2)), + (pd.Timestamp(dt1, tz=tz1), pd.Timestamp(dt2, tz=tz2)), + (pd.Timestamp(dt1, tz=tz2), pd.Timestamp(dt2, tz=tz1)) + ]) + def test_mismatching_tz_raises_err(self, start, end): #issue 18488 - dt1_tz = pd.Timestamp('2017-01-01', tz='US/Eastern') - dt1_no_tz = pd.Timestamp('2017-01-01') - dt2_tz = pd.Timestamp('2017-01-04', tz='US/Eastern') - dt2_no_tz = pd.Timestamp('2017-01-04') - dt1_diff_tz = pd.Timestamp('2017-01-01', tz='Europe/London') - dt2_diff_tz = pd.Timestamp('2017-01-04', tz='Europe/London') - - def assert_tz_raises_exception(start, end): - with pytest.raises(TypeError): - pd.date_range(start, end) - with pytest.raises(TypeError): - pd.DatetimeIndexstart(start, end) - - assert_tz_raises_exception(dt1_no_tz, dt2_tz) - assert_tz_raises_exception(dt1_tz, dt2_no_tz) - assert_tz_raises_exception(dt1_tz, dt2_diff_tz) - assert_tz_raises_exception(dt1_diff_tz, dt2_tz) + with pytest.raises(TypeError): + pd.date_range(start, end) + with pytest.raises(TypeError): + pd.DatetimeIndex(start, end, freq=BDay()) + class TestBusinessDateRange(object): From 53744647d7d8e8e23902f6515f26baad7704e131 Mon Sep 17 00:00:00 2001 From: aschade Date: Sun, 26 Nov 2017 08:57:31 -0500 Subject: [PATCH 7/8] TST: PEP8 fixes --- pandas/tests/indexes/datetimes/test_date_range.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/pandas/tests/indexes/datetimes/test_date_range.py b/pandas/tests/indexes/datetimes/test_date_range.py index 629a6681a9984..bbbdea65b6de2 100644 --- a/pandas/tests/indexes/datetimes/test_date_range.py +++ b/pandas/tests/indexes/datetimes/test_date_range.py @@ -290,7 +290,8 @@ def test_precision_finer_than_offset(self): tm.assert_index_equal(result1, expected1) tm.assert_index_equal(result2, expected2) - dt1, dt2, tz1, tz2 = '2017-01-01', '2017-01-01', 'US/Eastern', 'Europe/London' + dt1, dt2 = '2017-01-01', '2017-01-01' + tz1, tz2 = 'US/Eastern', 'Europe/London' @pytest.mark.parametrize("start,end", [ (pd.Timestamp(dt1, tz=tz1), pd.Timestamp(dt2)), (pd.Timestamp(dt1), pd.Timestamp(dt2, tz=tz2)), @@ -298,7 +299,7 @@ def test_precision_finer_than_offset(self): (pd.Timestamp(dt1, tz=tz2), pd.Timestamp(dt2, tz=tz1)) ]) def test_mismatching_tz_raises_err(self, start, end): - #issue 18488 + # issue 18488 with pytest.raises(TypeError): pd.date_range(start, end) with pytest.raises(TypeError): From 685b861be2958db1dad574dcaacba50d97135548 Mon Sep 17 00:00:00 2001 From: aschade Date: Sun, 26 Nov 2017 09:00:11 -0500 Subject: [PATCH 8/8] TST: A few more PEP8 fixes --- pandas/tests/indexes/datetimes/test_date_range.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/pandas/tests/indexes/datetimes/test_date_range.py b/pandas/tests/indexes/datetimes/test_date_range.py index bbbdea65b6de2..826e20b8b0586 100644 --- a/pandas/tests/indexes/datetimes/test_date_range.py +++ b/pandas/tests/indexes/datetimes/test_date_range.py @@ -292,9 +292,10 @@ def test_precision_finer_than_offset(self): dt1, dt2 = '2017-01-01', '2017-01-01' tz1, tz2 = 'US/Eastern', 'Europe/London' + @pytest.mark.parametrize("start,end", [ (pd.Timestamp(dt1, tz=tz1), pd.Timestamp(dt2)), - (pd.Timestamp(dt1), pd.Timestamp(dt2, tz=tz2)), + (pd.Timestamp(dt1), pd.Timestamp(dt2, tz=tz2)), (pd.Timestamp(dt1, tz=tz1), pd.Timestamp(dt2, tz=tz2)), (pd.Timestamp(dt1, tz=tz2), pd.Timestamp(dt2, tz=tz1)) ])