|
| 1 | +# -*- coding: utf-8 -*- |
| 2 | +""" |
| 3 | +Behavioral based tests for offsets and date_range. |
| 4 | +
|
| 5 | +This file is adapted from https://github.com/pandas-dev/pandas/pull/18761 - |
| 6 | +which was more ambitious but less idiomatic in its use of Hypothesis. |
| 7 | +
|
| 8 | +You may wish to consult the previous version for inspiration on further |
| 9 | +tests, or when trying to pin down the bugs exposed by the tests below. |
| 10 | +""" |
| 11 | + |
| 12 | +import pytest |
| 13 | +from hypothesis import given, assume, strategies as st |
| 14 | +from hypothesis.extra.pytz import timezones as pytz_timezones |
| 15 | +from hypothesis.extra.dateutil import timezones as dateutil_timezones |
| 16 | + |
| 17 | +import pandas as pd |
| 18 | + |
| 19 | +from pandas.tseries.offsets import ( |
| 20 | + MonthEnd, MonthBegin, BMonthEnd, BMonthBegin, |
| 21 | + QuarterEnd, QuarterBegin, BQuarterEnd, BQuarterBegin, |
| 22 | + YearEnd, YearBegin, BYearEnd, BYearBegin, |
| 23 | +) |
| 24 | + |
| 25 | +# ---------------------------------------------------------------- |
| 26 | +# Helpers for generating random data |
| 27 | + |
| 28 | +gen_date_range = st.builds( |
| 29 | + pd.date_range, |
| 30 | + start=st.datetimes( |
| 31 | + # TODO: Choose the min/max values more systematically |
| 32 | + min_value=pd.Timestamp(1900, 1, 1).to_pydatetime(), |
| 33 | + max_value=pd.Timestamp(2100, 1, 1).to_pydatetime() |
| 34 | + ), |
| 35 | + periods=st.integers(min_value=2, max_value=100), |
| 36 | + freq=st.sampled_from('Y Q M D H T s ms us ns'.split()), |
| 37 | + tz=st.one_of(st.none(), dateutil_timezones(), pytz_timezones()), |
| 38 | +) |
| 39 | + |
| 40 | +gen_random_datetime = st.datetimes( |
| 41 | + min_value=pd.Timestamp.min.to_pydatetime(), |
| 42 | + max_value=pd.Timestamp.max.to_pydatetime(), |
| 43 | + timezones=st.one_of(st.none(), dateutil_timezones(), pytz_timezones()) |
| 44 | +) |
| 45 | + |
| 46 | +# The strategy for each type is registered in conftest.py, as they don't carry |
| 47 | +# enough runtime information (e.g. type hints) to infer how to build them. |
| 48 | +gen_yqm_offset = st.one_of(*map(st.from_type, [ |
| 49 | + MonthBegin, MonthEnd, BMonthBegin, BMonthEnd, |
| 50 | + QuarterBegin, QuarterEnd, BQuarterBegin, BQuarterEnd, |
| 51 | + YearBegin, YearEnd, BYearBegin, BYearEnd |
| 52 | +])) |
| 53 | + |
| 54 | + |
| 55 | +# ---------------------------------------------------------------- |
| 56 | +# Offset-specific behaviour tests |
| 57 | + |
| 58 | + |
| 59 | +# Based on CI runs: Always passes on OSX, fails on Linux, sometimes on Windows |
| 60 | +@pytest.mark.xfail(strict=False, reason='inconsistent between OSs, Pythons') |
| 61 | +@given(gen_random_datetime, gen_yqm_offset) |
| 62 | +def test_on_offset_implementations(dt, offset): |
| 63 | + assume(not offset.normalize) |
| 64 | + # check that the class-specific implementations of onOffset match |
| 65 | + # the general case definition: |
| 66 | + # (dt + offset) - offset == dt |
| 67 | + compare = (dt + offset) - offset |
| 68 | + assert offset.onOffset(dt) == (compare == dt) |
| 69 | + |
| 70 | + |
| 71 | +@pytest.mark.xfail(strict=True) |
| 72 | +@given(gen_yqm_offset, gen_date_range) |
| 73 | +def test_apply_index_implementations(offset, rng): |
| 74 | + # offset.apply_index(dti)[i] should match dti[i] + offset |
| 75 | + assume(offset.n != 0) # TODO: test for that case separately |
| 76 | + |
| 77 | + # rng = pd.date_range(start='1/1/2000', periods=100000, freq='T') |
| 78 | + ser = pd.Series(rng) |
| 79 | + |
| 80 | + res = rng + offset |
| 81 | + res_v2 = offset.apply_index(rng) |
| 82 | + assert (res == res_v2).all() |
| 83 | + |
| 84 | + assert res[0] == rng[0] + offset |
| 85 | + assert res[-1] == rng[-1] + offset |
| 86 | + res2 = ser + offset |
| 87 | + # apply_index is only for indexes, not series, so no res2_v2 |
| 88 | + assert res2.iloc[0] == ser.iloc[0] + offset |
| 89 | + assert res2.iloc[-1] == ser.iloc[-1] + offset |
| 90 | + # TODO: Check randomly assorted entries, not just first/last |
| 91 | + |
| 92 | + |
| 93 | +@pytest.mark.xfail(strict=True) |
| 94 | +@given(gen_yqm_offset) |
| 95 | +def test_shift_across_dst(offset): |
| 96 | + # GH#18319 check that 1) timezone is correctly normalized and |
| 97 | + # 2) that hour is not incorrectly changed by this normalization |
| 98 | + # Note that dti includes a transition across DST boundary |
| 99 | + dti = pd.date_range(start='2017-10-30 12:00:00', end='2017-11-06', |
| 100 | + freq='D', tz='US/Eastern') |
| 101 | + assert (dti.hour == 12).all() # we haven't screwed up yet |
| 102 | + |
| 103 | + res = dti + offset |
| 104 | + assert (res.hour == 12).all() |
0 commit comments