test_offsets_properties.py 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. """
  2. Behavioral based tests for offsets and date_range.
  3. This file is adapted from https://github.com/pandas-dev/pandas/pull/18761 -
  4. which was more ambitious but less idiomatic in its use of Hypothesis.
  5. You may wish to consult the previous version for inspiration on further
  6. tests, or when trying to pin down the bugs exposed by the tests below.
  7. """
  8. from hypothesis import (
  9. assume,
  10. given,
  11. )
  12. import pytest
  13. import pytz
  14. import pandas as pd
  15. from pandas._testing._hypothesis import (
  16. DATETIME_JAN_1_1900_OPTIONAL_TZ,
  17. YQM_OFFSET,
  18. )
  19. # ----------------------------------------------------------------
  20. # Offset-specific behaviour tests
  21. @pytest.mark.arm_slow
  22. @given(DATETIME_JAN_1_1900_OPTIONAL_TZ, YQM_OFFSET)
  23. def test_on_offset_implementations(dt, offset):
  24. assume(not offset.normalize)
  25. # check that the class-specific implementations of is_on_offset match
  26. # the general case definition:
  27. # (dt + offset) - offset == dt
  28. try:
  29. compare = (dt + offset) - offset
  30. except (pytz.NonExistentTimeError, pytz.AmbiguousTimeError):
  31. # When dt + offset does not exist or is DST-ambiguous, assume(False) to
  32. # indicate to hypothesis that this is not a valid test case
  33. # DST-ambiguous example (GH41906):
  34. # dt = datetime.datetime(1900, 1, 1, tzinfo=pytz.timezone('Africa/Kinshasa'))
  35. # offset = MonthBegin(66)
  36. assume(False)
  37. assert offset.is_on_offset(dt) == (compare == dt)
  38. @given(YQM_OFFSET)
  39. def test_shift_across_dst(offset):
  40. # GH#18319 check that 1) timezone is correctly normalized and
  41. # 2) that hour is not incorrectly changed by this normalization
  42. assume(not offset.normalize)
  43. # Note that dti includes a transition across DST boundary
  44. dti = pd.date_range(
  45. start="2017-10-30 12:00:00", end="2017-11-06", freq="D", tz="US/Eastern"
  46. )
  47. assert (dti.hour == 12).all() # we haven't screwed up yet
  48. res = dti + offset
  49. assert (res.hour == 12).all()