test_calendar.py 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. from datetime import datetime
  2. import pytest
  3. from pandas import (
  4. DatetimeIndex,
  5. offsets,
  6. to_datetime,
  7. )
  8. import pandas._testing as tm
  9. from pandas.tseries.holiday import (
  10. AbstractHolidayCalendar,
  11. Holiday,
  12. Timestamp,
  13. USFederalHolidayCalendar,
  14. USLaborDay,
  15. USThanksgivingDay,
  16. get_calendar,
  17. )
  18. @pytest.mark.parametrize(
  19. "transform", [lambda x: x, lambda x: x.strftime("%Y-%m-%d"), lambda x: Timestamp(x)]
  20. )
  21. def test_calendar(transform):
  22. start_date = datetime(2012, 1, 1)
  23. end_date = datetime(2012, 12, 31)
  24. calendar = USFederalHolidayCalendar()
  25. holidays = calendar.holidays(transform(start_date), transform(end_date))
  26. expected = [
  27. datetime(2012, 1, 2),
  28. datetime(2012, 1, 16),
  29. datetime(2012, 2, 20),
  30. datetime(2012, 5, 28),
  31. datetime(2012, 7, 4),
  32. datetime(2012, 9, 3),
  33. datetime(2012, 10, 8),
  34. datetime(2012, 11, 12),
  35. datetime(2012, 11, 22),
  36. datetime(2012, 12, 25),
  37. ]
  38. assert list(holidays.to_pydatetime()) == expected
  39. def test_calendar_caching():
  40. # see gh-9552.
  41. class TestCalendar(AbstractHolidayCalendar):
  42. def __init__(self, name=None, rules=None) -> None:
  43. super().__init__(name=name, rules=rules)
  44. jan1 = TestCalendar(rules=[Holiday("jan1", year=2015, month=1, day=1)])
  45. jan2 = TestCalendar(rules=[Holiday("jan2", year=2015, month=1, day=2)])
  46. # Getting holidays for Jan 1 should not alter results for Jan 2.
  47. tm.assert_index_equal(jan1.holidays(), DatetimeIndex(["01-Jan-2015"]))
  48. tm.assert_index_equal(jan2.holidays(), DatetimeIndex(["02-Jan-2015"]))
  49. def test_calendar_observance_dates():
  50. # see gh-11477
  51. us_fed_cal = get_calendar("USFederalHolidayCalendar")
  52. holidays0 = us_fed_cal.holidays(
  53. datetime(2015, 7, 3), datetime(2015, 7, 3)
  54. ) # <-- same start and end dates
  55. holidays1 = us_fed_cal.holidays(
  56. datetime(2015, 7, 3), datetime(2015, 7, 6)
  57. ) # <-- different start and end dates
  58. holidays2 = us_fed_cal.holidays(
  59. datetime(2015, 7, 3), datetime(2015, 7, 3)
  60. ) # <-- same start and end dates
  61. # These should all produce the same result.
  62. #
  63. # In addition, calling with different start and end
  64. # dates should not alter the output if we call the
  65. # function again with the same start and end date.
  66. tm.assert_index_equal(holidays0, holidays1)
  67. tm.assert_index_equal(holidays0, holidays2)
  68. def test_rule_from_name():
  69. us_fed_cal = get_calendar("USFederalHolidayCalendar")
  70. assert us_fed_cal.rule_from_name("Thanksgiving Day") == USThanksgivingDay
  71. def test_calendar_2031():
  72. # See gh-27790
  73. #
  74. # Labor Day 2031 is on September 1. Saturday before is August 30.
  75. # Next working day after August 30 ought to be Tuesday, September 2.
  76. class testCalendar(AbstractHolidayCalendar):
  77. rules = [USLaborDay]
  78. cal = testCalendar()
  79. workDay = offsets.CustomBusinessDay(calendar=cal)
  80. Sat_before_Labor_Day_2031 = to_datetime("2031-08-30")
  81. next_working_day = Sat_before_Labor_Day_2031 + 0 * workDay
  82. assert next_working_day == to_datetime("2031-09-02")
  83. def test_no_holidays_calendar():
  84. # Test for issue #31415
  85. class NoHolidaysCalendar(AbstractHolidayCalendar):
  86. pass
  87. cal = NoHolidaysCalendar()
  88. holidays = cal.holidays(Timestamp("01-Jan-2020"), Timestamp("01-Jan-2021"))
  89. empty_index = DatetimeIndex([]) # Type is DatetimeIndex since return_name=False
  90. tm.assert_index_equal(holidays, empty_index)