Skip to content

TST: lock down timeseries now tests, xref #18666 #18709

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 5 commits into from
Dec 12, 2017
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
4 changes: 2 additions & 2 deletions pandas/_libs/tslib.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,7 @@ def _test_parse_iso8601(object ts):
if ts == 'now':
return Timestamp.utcnow()
elif ts == 'today':
return Timestamp.utcnow().normalize()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

side issue, do we really need this actual test routine? can't we just inspect the actual output?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Best guess is that this is intended to be double-sure that a test specifically hits string_to_dts (since coverage is hard with cython). I'd have no real objection to losing this function.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yeah i think ok to remove

return Timestamp.now().normalize()

_string_to_dts(ts, &obj.dts, &out_local, &out_tzoffset)
obj.value = dtstruct_to_dt64(&obj.dts)
Expand Down Expand Up @@ -734,7 +734,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

Expand Down
51 changes: 51 additions & 0 deletions pandas/tests/indexes/datetimes/test_tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -187,6 +188,56 @@ 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
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; 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

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
# 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; 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

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; 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

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 = [
Expand Down