Skip to content

Commit 38c6fc8

Browse files
jbrockmendeljreback
authored andcommitted
TST: lock down timeseries now tests, xref #18666 (#18709)
1 parent 4091f64 commit 38c6fc8

File tree

2 files changed

+53
-2
lines changed

2 files changed

+53
-2
lines changed

pandas/_libs/tslib.pyx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -220,7 +220,7 @@ def _test_parse_iso8601(object ts):
220220
if ts == 'now':
221221
return Timestamp.utcnow()
222222
elif ts == 'today':
223-
return Timestamp.utcnow().normalize()
223+
return Timestamp.now().normalize()
224224

225225
_string_to_dts(ts, &obj.dts, &out_local, &out_tzoffset)
226226
obj.value = dtstruct_to_dt64(&obj.dts)
@@ -734,7 +734,7 @@ cdef inline bint _parse_today_now(str val, int64_t* iresult):
734734
return True
735735
elif val == 'today':
736736
# Note: this is *not* the same as Timestamp('today')
737-
iresult[0] = Timestamp.utcnow().normalize().value
737+
iresult[0] = Timestamp.now().normalize().value
738738
return True
739739
return False
740740

pandas/tests/indexes/datetimes/test_tools.py

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
from pandas.compat.numpy import np_array_datetime64_compat
2222
from pandas.core.dtypes.common import is_datetime64_ns_dtype
2323
from pandas.util import testing as tm
24+
import pandas.util._test_decorators as td
2425
from pandas.util.testing import assert_series_equal, _skip_if_has_locale
2526
from pandas import (isna, to_datetime, Timestamp, Series, DataFrame,
2627
Index, DatetimeIndex, NaT, date_range, compat)
@@ -187,6 +188,56 @@ def test_to_datetime_format_weeks(self, cache):
187188

188189
class TestToDatetime(object):
189190

191+
@td.skip_if_windows # `tm.set_timezone` does not work in windows
192+
def test_to_datetime_now(self):
193+
# See GH#18666
194+
with tm.set_timezone('US/Eastern'):
195+
npnow = np.datetime64('now').astype('datetime64[ns]')
196+
pdnow = pd.to_datetime('now')
197+
pdnow2 = pd.to_datetime(['now'])[0]
198+
199+
# These should all be equal with infinite perf; this gives
200+
# a generous margin of 10 seconds
201+
assert abs(pdnow.value - npnow.astype(np.int64)) < 1e10
202+
assert abs(pdnow2.value - npnow.astype(np.int64)) < 1e10
203+
204+
assert pdnow.tzinfo is None
205+
assert pdnow2.tzinfo is None
206+
207+
@td.skip_if_windows # `tm.set_timezone` does not work in windows
208+
def test_to_datetime_today(self):
209+
# See GH#18666
210+
# Test with one timezone far ahead of UTC and another far behind, so
211+
# one of these will _almost_ alawys be in a different day from UTC.
212+
# Unfortunately this test between 12 and 1 AM Samoa time
213+
# this both of these timezones _and_ UTC will all be in the same day,
214+
# so this test will not detect the regression introduced in #18666.
215+
with tm.set_timezone('Pacific/Auckland'): # 12-13 hours ahead of UTC
216+
nptoday = np.datetime64('today').astype('datetime64[ns]')
217+
pdtoday = pd.to_datetime('today')
218+
pdtoday2 = pd.to_datetime(['today'])[0]
219+
220+
# These should all be equal with infinite perf; this gives
221+
# a generous margin of 10 seconds
222+
assert abs(pdtoday.value - nptoday.astype(np.int64)) < 1e10
223+
assert abs(pdtoday2.value - nptoday.astype(np.int64)) < 1e10
224+
225+
assert pdtoday.tzinfo is None
226+
assert pdtoday2.tzinfo is None
227+
228+
with tm.set_timezone('US/Samoa'): # 11 hours behind UTC
229+
nptoday = np.datetime64('today').astype('datetime64[ns]')
230+
pdtoday = pd.to_datetime('today')
231+
pdtoday2 = pd.to_datetime(['today'])[0]
232+
233+
# These should all be equal with infinite perf; this gives
234+
# a generous margin of 10 seconds
235+
assert abs(pdtoday.value - nptoday.astype(np.int64)) < 1e10
236+
assert abs(pdtoday2.value - nptoday.astype(np.int64)) < 1e10
237+
238+
assert pdtoday.tzinfo is None
239+
assert pdtoday2.tzinfo is None
240+
190241
@pytest.mark.parametrize('cache', [True, False])
191242
def test_to_datetime_dt64s(self, cache):
192243
in_bound_dts = [

0 commit comments

Comments
 (0)