From 75fadb8c817e3c7f149782f7294dc35fb412d02d Mon Sep 17 00:00:00 2001 From: Brock Mendel Date: Sat, 9 Dec 2017 19:09:17 -0800 Subject: [PATCH 1/4] fix change in 18666, add tests --- pandas/_libs/tslib.pyx | 4 +- pandas/tests/indexes/datetimes/test_tools.py | 42 ++++++++++++++++++++ 2 files changed, 44 insertions(+), 2 deletions(-) diff --git a/pandas/_libs/tslib.pyx b/pandas/_libs/tslib.pyx index 293e10d1934fa..4a00f5ae78d44 100644 --- a/pandas/_libs/tslib.pyx +++ b/pandas/_libs/tslib.pyx @@ -211,7 +211,7 @@ def _test_parse_iso8601(object ts): if ts == 'now': return Timestamp.utcnow() elif ts == 'today': - return Timestamp.utcnow().normalize() + return Timestamp.now().normalize() _string_to_dts(ts, &obj.dts, &out_local, &out_tzoffset) obj.value = dtstruct_to_dt64(&obj.dts) @@ -725,7 +725,7 @@ cdef inline bint _parse_today_now(str val, int64_t* iresult): return True elif val == 'today': # Note: this is *not* the same as Timestamp('today') - iresult[0] = Timestamp.utcnow().normalize().value + iresult[0] = Timestamp.now().normalize().value return True return False diff --git a/pandas/tests/indexes/datetimes/test_tools.py b/pandas/tests/indexes/datetimes/test_tools.py index bdee67a4ff674..f7311abb38c64 100644 --- a/pandas/tests/indexes/datetimes/test_tools.py +++ b/pandas/tests/indexes/datetimes/test_tools.py @@ -187,6 +187,48 @@ def test_to_datetime_format_weeks(self, cache): class TestToDatetime(object): + def test_to_datetime_now(self): + # See GH#18666 + with tm.set_timezone('US/Eastern'): + npnow = np.datetime64('now').astype('datetime64[ns]') + pdnow = pd.to_datetime('now') + pdnow2 = pd.to_datetime(['now'])[0] + # These should all be equal with infinite perf + assert abs(pdnow.value - npnow.astype(np.int64)) < 1e10 + assert abs(pdnow2.value - npnow.astype(np.int64)) < 1e10 + + assert pdnow.tzinfo is None + assert pdnow2.tzinfo is None + + def test_to_datetime_today(self): + # See GH#18666 + # Test with one timezone far ahead of UTC and another far behind, so + # one of these will _almost_ alawys be in a different day from UTC. + # Unfortunately this test between 12 and 1 AM Samoa time + # this both of these timezones _and_ UTC will all be in the same day, + # so this test will not detect the regression introduced in #18666. + with tm.set_timezone('Pacific/Auckland'): # 12-13 hours ahead of UTC + nptoday = np.datetime64('today').astype('datetime64[ns]') + pdtoday = pd.to_datetime('today') + pdtoday2 = pd.to_datetime(['today'])[0] + # These should all be equal with infinite perf + assert abs(pdtoday.value - nptoday.astype(np.int64)) < 1e10 + assert abs(pdtoday2.value - nptoday.astype(np.int64)) < 1e10 + + assert pdtoday.tzinfo is None + assert pdtoday2.tzinfo is None + + with tm.set_timezone('US/Samoa'): # 11 hours behind UTC + nptoday = np.datetime64('today').astype('datetime64[ns]') + pdtoday = pd.to_datetime('today') + pdtoday2 = pd.to_datetime(['today'])[0] + # These should all be equal with infinite perf + assert abs(pdtoday.value - nptoday.astype(np.int64)) < 1e10 + assert abs(pdtoday2.value - nptoday.astype(np.int64)) < 1e10 + + assert pdtoday.tzinfo is None + assert pdtoday2.tzinfo is None + @pytest.mark.parametrize('cache', [True, False]) def test_to_datetime_dt64s(self, cache): in_bound_dts = [ From e9225000fa32024bff8e1dc80e920171dbb16ef8 Mon Sep 17 00:00:00 2001 From: Brock Mendel Date: Sun, 10 Dec 2017 19:08:35 -0800 Subject: [PATCH 2/4] edit comments per reviewer request --- pandas/tests/indexes/datetimes/test_tools.py | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/pandas/tests/indexes/datetimes/test_tools.py b/pandas/tests/indexes/datetimes/test_tools.py index f7311abb38c64..d59936870fd3f 100644 --- a/pandas/tests/indexes/datetimes/test_tools.py +++ b/pandas/tests/indexes/datetimes/test_tools.py @@ -193,7 +193,9 @@ def test_to_datetime_now(self): npnow = np.datetime64('now').astype('datetime64[ns]') pdnow = pd.to_datetime('now') pdnow2 = pd.to_datetime(['now'])[0] - # These should all be equal with infinite perf + + # These should all be equal with infinite perf; this gives + # a generous margin of 10 seconds assert abs(pdnow.value - npnow.astype(np.int64)) < 1e10 assert abs(pdnow2.value - npnow.astype(np.int64)) < 1e10 @@ -211,7 +213,9 @@ def test_to_datetime_today(self): nptoday = np.datetime64('today').astype('datetime64[ns]') pdtoday = pd.to_datetime('today') pdtoday2 = pd.to_datetime(['today'])[0] - # These should all be equal with infinite perf + + # These should all be equal with infinite perf; this gives + # a generous margin of 10 seconds assert abs(pdtoday.value - nptoday.astype(np.int64)) < 1e10 assert abs(pdtoday2.value - nptoday.astype(np.int64)) < 1e10 @@ -222,7 +226,9 @@ def test_to_datetime_today(self): nptoday = np.datetime64('today').astype('datetime64[ns]') pdtoday = pd.to_datetime('today') pdtoday2 = pd.to_datetime(['today'])[0] - # These should all be equal with infinite perf + + # These should all be equal with infinite perf; this gives + # a generous margin of 10 seconds assert abs(pdtoday.value - nptoday.astype(np.int64)) < 1e10 assert abs(pdtoday2.value - nptoday.astype(np.int64)) < 1e10 From bd9b723e230bea12c8c02f77a7b12c647bdf2075 Mon Sep 17 00:00:00 2001 From: Brock Mendel Date: Mon, 11 Dec 2017 13:21:15 -0800 Subject: [PATCH 3/4] skip on windows --- pandas/tests/indexes/datetimes/test_tools.py | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/pandas/tests/indexes/datetimes/test_tools.py b/pandas/tests/indexes/datetimes/test_tools.py index d59936870fd3f..b007ff928230d 100644 --- a/pandas/tests/indexes/datetimes/test_tools.py +++ b/pandas/tests/indexes/datetimes/test_tools.py @@ -189,6 +189,8 @@ class TestToDatetime(object): def test_to_datetime_now(self): # See GH#18666 + tm._skip_if_windows() # `tm.set_timezone` does not work in windows + with tm.set_timezone('US/Eastern'): npnow = np.datetime64('now').astype('datetime64[ns]') pdnow = pd.to_datetime('now') @@ -209,6 +211,8 @@ def test_to_datetime_today(self): # Unfortunately this test between 12 and 1 AM Samoa time # this both of these timezones _and_ UTC will all be in the same day, # so this test will not detect the regression introduced in #18666. + tm._skip_if_windows() # `tm.set_timezone` does not work in windows + with tm.set_timezone('Pacific/Auckland'): # 12-13 hours ahead of UTC nptoday = np.datetime64('today').astype('datetime64[ns]') pdtoday = pd.to_datetime('today') From af66e441394ce037077bad7d33e39fe258d1c8de Mon Sep 17 00:00:00 2001 From: Brock Mendel Date: Mon, 11 Dec 2017 15:05:48 -0800 Subject: [PATCH 4/4] update to use new skip_if_windows decorator --- pandas/tests/indexes/datetimes/test_tools.py | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/pandas/tests/indexes/datetimes/test_tools.py b/pandas/tests/indexes/datetimes/test_tools.py index b007ff928230d..a94865d8e9657 100644 --- a/pandas/tests/indexes/datetimes/test_tools.py +++ b/pandas/tests/indexes/datetimes/test_tools.py @@ -21,6 +21,7 @@ from pandas.compat.numpy import np_array_datetime64_compat from pandas.core.dtypes.common import is_datetime64_ns_dtype from pandas.util import testing as tm +import pandas.util._test_decorators as td from pandas.util.testing import assert_series_equal, _skip_if_has_locale from pandas import (isna, to_datetime, Timestamp, Series, DataFrame, Index, DatetimeIndex, NaT, date_range, compat) @@ -187,10 +188,9 @@ def test_to_datetime_format_weeks(self, cache): class TestToDatetime(object): + @td.skip_if_windows # `tm.set_timezone` does not work in windows def test_to_datetime_now(self): # See GH#18666 - tm._skip_if_windows() # `tm.set_timezone` does not work in windows - with tm.set_timezone('US/Eastern'): npnow = np.datetime64('now').astype('datetime64[ns]') pdnow = pd.to_datetime('now') @@ -204,6 +204,7 @@ def test_to_datetime_now(self): assert pdnow.tzinfo is None assert pdnow2.tzinfo is None + @td.skip_if_windows # `tm.set_timezone` does not work in windows def test_to_datetime_today(self): # See GH#18666 # Test with one timezone far ahead of UTC and another far behind, so @@ -211,8 +212,6 @@ def test_to_datetime_today(self): # Unfortunately this test between 12 and 1 AM Samoa time # this both of these timezones _and_ UTC will all be in the same day, # so this test will not detect the regression introduced in #18666. - tm._skip_if_windows() # `tm.set_timezone` does not work in windows - with tm.set_timezone('Pacific/Auckland'): # 12-13 hours ahead of UTC nptoday = np.datetime64('today').astype('datetime64[ns]') pdtoday = pd.to_datetime('today')