Example #1
0
    def check_results(self, holiday, start, end, expected):
        assert list(holiday.dates(start, end)) == expected

        # Verify that timezone info is preserved.
        assert (list(holiday.dates(utc.localize(Timestamp(start)),
                                   utc.localize(Timestamp(end)))) ==
                [utc.localize(dt) for dt in expected])
def test_holiday_dates(holiday, start_date, end_date, expected):
    assert list(holiday.dates(start_date, end_date)) == expected

    # Verify that timezone info is preserved.
    assert list(
        holiday.dates(
            utc.localize(Timestamp(start_date)), utc.localize(Timestamp(end_date))
        )
    ) == [utc.localize(dt) for dt in expected]
Example #3
0
def test_no_holidays_calendar():
    # Test for issue #31415

    class NoHolidaysCalendar(AbstractHolidayCalendar):
        pass

    cal = NoHolidaysCalendar()
    holidays = cal.holidays(Timestamp("01-Jan-2020"), Timestamp("01-Jan-2021"))
    empty_index = DatetimeIndex([])  # Type is DatetimeIndex since return_name=False
    tm.assert_index_equal(holidays, empty_index)
Example #4
0
 def check_results(self, holiday, start, end, expected):
     self.assertEqual(list(holiday.dates(start, end)), expected)
     # Verify that timezone info is preserved.
     self.assertEqual(
         list(
             holiday.dates(
                 utc.localize(Timestamp(start)),
                 utc.localize(Timestamp(end)),
             )),
         [utc.localize(dt) for dt in expected],
     )
Example #5
0
    def test_argument_types(self):
        holidays = USThanksgivingDay.dates(self.start_date, self.end_date)

        holidays_1 = USThanksgivingDay.dates(
            self.start_date.strftime('%Y-%m-%d'),
            self.end_date.strftime('%Y-%m-%d'))

        holidays_2 = USThanksgivingDay.dates(Timestamp(self.start_date),
                                             Timestamp(self.end_date))

        tm.assert_index_equal(holidays, holidays_1)
        tm.assert_index_equal(holidays, holidays_2)
Example #6
0
    def test_calendar(self):

        calendar = USFederalHolidayCalendar()
        holidays = calendar.holidays(self.start_date, self.end_date)

        holidays_1 = calendar.holidays(self.start_date.strftime('%Y-%m-%d'),
                                       self.end_date.strftime('%Y-%m-%d'))
        holidays_2 = calendar.holidays(Timestamp(self.start_date),
                                       Timestamp(self.end_date))

        assert list(holidays.to_pydatetime()) == self.holiday_list
        assert list(holidays_1.to_pydatetime()) == self.holiday_list
        assert list(holidays_2.to_pydatetime()) == self.holiday_list
def test_holidays_within_dates(holiday, start, expected):
    # see gh-11477
    #
    # Fix holiday behavior where holiday.dates returned dates outside
    # start/end date, or observed rules could not be applied because the
    # holiday was not in the original date range (e.g., 7/4/2015 -> 7/3/2015).
    if isinstance(holiday, str):
        calendar = get_calendar("USFederalHolidayCalendar")
        holiday = calendar.rule_from_name(holiday)

    assert list(holiday.dates(start, start)) == expected

    # Verify that timezone info is preserved.
    assert list(
        holiday.dates(utc.localize(Timestamp(start)), utc.localize(Timestamp(start)))
    ) == [utc.localize(dt) for dt in expected]
Example #8
0
def _check_holiday_results(holiday, start, end, expected):
    """
    Check that the dates for a given holiday match in date and timezone.

    Parameters
    ----------
    holiday : Holiday
        The holiday to check.
    start : datetime-like
        The start date of range in which to collect dates for a given holiday.
    end : datetime-like
        The end date of range in which to collect dates for a given holiday.
    expected : list
        The list of dates we expect to get.
    """
    assert list(holiday.dates(start, end)) == expected

    # Verify that timezone info is preserved.
    assert (list(holiday.dates(utc.localize(Timestamp(start)),
                               utc.localize(Timestamp(end)))) ==
            [utc.localize(dt) for dt in expected])
Example #9
0
def test_holidays_within_dates(holiday, start, expected):
    # see gh-11477
    #
    # Fix holiday behavior where holiday.dates returned dates outside
    # start/end date, or observed rules could not be applied because the
    # holiday was not in the original date range (e.g., 7/4/2015 -> 7/3/2015).
    if isinstance(holiday, str):
        calendar = get_calendar("USFederalHolidayCalendar")
        holiday = calendar.rule_from_name(holiday)

    if isinstance(expected, str):
        expected = [Timestamp(expected)]

    _check_holiday_results(holiday, start, start, expected)
Example #10
0
 def test_non_observed_holiday(self):
     july_3rd = Holiday('July 4th Eve', month=7,  day=3)
     result = july_3rd.dates("2001-01-01", "2003-03-03")
     expected = [Timestamp('2001-07-03 00:00:00'),
                 Timestamp('2002-07-03 00:00:00')]
     self.assertEqual(list(result), expected)
     july_3rd = Holiday('July 4th Eve', month=7,  day=3, 
                        days_of_week=(0, 1, 2, 3))
     result = july_3rd.dates("2001-01-01", "2008-03-03")
     expected = [Timestamp('2001-07-03 00:00:00'),
                 Timestamp('2002-07-03 00:00:00'),
                 Timestamp('2003-07-03 00:00:00'),
                 Timestamp('2006-07-03 00:00:00'),
                 Timestamp('2007-07-03 00:00:00')]
     self.assertEqual(list(result), expected)
Example #11
0
    def test_holidays_within_dates(self):
        # Fix holiday behavior found in #11477
        # where holiday.dates returned dates outside start/end date
        # or observed rules could not be applied as the holiday
        # was not in the original date range (e.g., 7/4/2015 -> 7/3/2015)
        start_date = datetime(2015, 7, 1)
        end_date = datetime(2015, 7, 1)

        calendar = get_calendar('USFederalHolidayCalendar')
        new_years = calendar.rule_from_name('New Years Day')
        july_4th = calendar.rule_from_name('July 4th')
        veterans_day = calendar.rule_from_name('Veterans Day')
        christmas = calendar.rule_from_name('Christmas')

        # Holiday: (start/end date, holiday)
        holidays = {USMemorialDay: ("2015-05-25", "2015-05-25"),
                    USLaborDay: ("2015-09-07", "2015-09-07"),
                    USColumbusDay: ("2015-10-12", "2015-10-12"),
                    USThanksgivingDay: ("2015-11-26", "2015-11-26"),
                    USMartinLutherKingJr: ("2015-01-19", "2015-01-19"),
                    USPresidentsDay: ("2015-02-16", "2015-02-16"),
                    GoodFriday: ("2015-04-03", "2015-04-03"),
                    EasterMonday: [("2015-04-06", "2015-04-06"),
                                   ("2015-04-05", [])],
                    new_years: [("2015-01-01", "2015-01-01"),
                                ("2011-01-01", []),
                                ("2010-12-31", "2010-12-31")],
                    july_4th: [("2015-07-03", "2015-07-03"),
                               ("2015-07-04", [])],
                    veterans_day: [("2012-11-11", []),
                                   ("2012-11-12", "2012-11-12")],
                    christmas: [("2011-12-25", []),
                                ("2011-12-26", "2011-12-26")]}

        for rule, dates in compat.iteritems(holidays):
            empty_dates = rule.dates(start_date, end_date)
            assert empty_dates.tolist() == []

            if isinstance(dates, tuple):
                dates = [dates]

            for start, expected in dates:
                if len(expected):
                    expected = [Timestamp(expected)]
                self.check_results(rule, start, start, expected)
Example #12
0
    def test_non_observed_holiday(self):

        self.check_results(Holiday('July 4th Eve', month=7, day=3),
                           start="2001-01-01",
                           end="2003-03-03",
                           expected=[
                               Timestamp('2001-07-03 00:00:00'),
                               Timestamp('2002-07-03 00:00:00')
                           ])

        self.check_results(Holiday('July 4th Eve',
                                   month=7,
                                   day=3,
                                   days_of_week=(0, 1, 2, 3)),
                           start="2001-01-01",
                           end="2008-03-03",
                           expected=[
                               Timestamp('2001-07-03 00:00:00'),
                               Timestamp('2002-07-03 00:00:00'),
                               Timestamp('2003-07-03 00:00:00'),
                               Timestamp('2006-07-03 00:00:00'),
                               Timestamp('2007-07-03 00:00:00'),
                           ])
Example #13
0
    def test_easter(self):

        self.check_results(
            EasterMonday,
            start=self.start_date,
            end=self.end_date,
            expected=[
                Timestamp('2011-04-25 00:00:00'),
                Timestamp('2012-04-09 00:00:00'),
                Timestamp('2013-04-01 00:00:00'),
                Timestamp('2014-04-21 00:00:00'),
                Timestamp('2015-04-06 00:00:00'),
                Timestamp('2016-03-28 00:00:00'),
                Timestamp('2017-04-17 00:00:00'),
                Timestamp('2018-04-02 00:00:00'),
                Timestamp('2019-04-22 00:00:00'),
                Timestamp('2020-04-13 00:00:00'),
            ],
        )
        self.check_results(
            GoodFriday,
            start=self.start_date,
            end=self.end_date,
            expected=[
                Timestamp('2011-04-22 00:00:00'),
                Timestamp('2012-04-06 00:00:00'),
                Timestamp('2013-03-29 00:00:00'),
                Timestamp('2014-04-18 00:00:00'),
                Timestamp('2015-04-03 00:00:00'),
                Timestamp('2016-03-25 00:00:00'),
                Timestamp('2017-04-14 00:00:00'),
                Timestamp('2018-03-30 00:00:00'),
                Timestamp('2019-04-19 00:00:00'),
                Timestamp('2020-04-10 00:00:00'),
            ],
        )
Example #14
0
    # Verify that timezone info is preserved.
    assert (list(holiday.dates(utc.localize(Timestamp(start)),
                               utc.localize(Timestamp(end)))) ==
            [utc.localize(dt) for dt in expected])


@pytest.mark.parametrize("holiday,start_date,end_date,expected", [
    (USMemorialDay, datetime(2011, 1, 1), datetime(2020, 12, 31),
     [datetime(2011, 5, 30), datetime(2012, 5, 28), datetime(2013, 5, 27),
      datetime(2014, 5, 26), datetime(2015, 5, 25), datetime(2016, 5, 30),
      datetime(2017, 5, 29), datetime(2018, 5, 28), datetime(2019, 5, 27),
      datetime(2020, 5, 25)]),

    (Holiday("July 4th Eve", month=7, day=3), "2001-01-01", "2003-03-03",
     [Timestamp("2001-07-03 00:00:00"), Timestamp("2002-07-03 00:00:00")]),
    (Holiday("July 4th Eve", month=7, day=3, days_of_week=(0, 1, 2, 3)),
     "2001-01-01", "2008-03-03", [
         Timestamp("2001-07-03 00:00:00"), Timestamp("2002-07-03 00:00:00"),
         Timestamp("2003-07-03 00:00:00"), Timestamp("2006-07-03 00:00:00"),
         Timestamp("2007-07-03 00:00:00")]),

    (EasterMonday, datetime(2011, 1, 1), datetime(2020, 12, 31),
     [Timestamp("2011-04-25 00:00:00"), Timestamp("2012-04-09 00:00:00"),
      Timestamp("2013-04-01 00:00:00"), Timestamp("2014-04-21 00:00:00"),
      Timestamp("2015-04-06 00:00:00"), Timestamp("2016-03-28 00:00:00"),
      Timestamp("2017-04-17 00:00:00"), Timestamp("2018-04-02 00:00:00"),
      Timestamp("2019-04-22 00:00:00"), Timestamp("2020-04-13 00:00:00")]),
    (GoodFriday, datetime(2011, 1, 1), datetime(2020, 12, 31),
     [Timestamp("2011-04-22 00:00:00"), Timestamp("2012-04-06 00:00:00"),
      Timestamp("2013-03-29 00:00:00"), Timestamp("2014-04-18 00:00:00"),
Example #15
0
from pandas import DatetimeIndex, offsets, to_datetime
import pandas._testing as tm

from pandas.tseries.holiday import (
    AbstractHolidayCalendar,
    Holiday,
    Timestamp,
    USFederalHolidayCalendar,
    USLaborDay,
    USThanksgivingDay,
    get_calendar,
)


@pytest.mark.parametrize(
    "transform", [lambda x: x, lambda x: x.strftime("%Y-%m-%d"), lambda x: Timestamp(x)]
)
def test_calendar(transform):
    start_date = datetime(2012, 1, 1)
    end_date = datetime(2012, 12, 31)

    calendar = USFederalHolidayCalendar()
    holidays = calendar.holidays(transform(start_date), transform(end_date))

    expected = [
        datetime(2012, 1, 2),
        datetime(2012, 1, 16),
        datetime(2012, 2, 20),
        datetime(2012, 5, 28),
        datetime(2012, 7, 4),
        datetime(2012, 9, 3),
Example #16
0
    def test_easter(self):
        holidays = EasterMonday.dates(self.start_date,
                                      self.end_date)
        holidayList = [Timestamp('2011-04-25 00:00:00'),
                       Timestamp('2012-04-09 00:00:00'),
                       Timestamp('2013-04-01 00:00:00'),
                       Timestamp('2014-04-21 00:00:00'),
                       Timestamp('2015-04-06 00:00:00'),
                       Timestamp('2016-03-28 00:00:00'),
                       Timestamp('2017-04-17 00:00:00'),
                       Timestamp('2018-04-02 00:00:00'),
                       Timestamp('2019-04-22 00:00:00'),
                       Timestamp('2020-04-13 00:00:00')]


        self.assertEqual(list(holidays), holidayList)
        holidays = GoodFriday.dates(self.start_date,
                                    self.end_date)
        holidayList = [Timestamp('2011-04-22 00:00:00'),
                       Timestamp('2012-04-06 00:00:00'),
                       Timestamp('2013-03-29 00:00:00'),
                       Timestamp('2014-04-18 00:00:00'),
                       Timestamp('2015-04-03 00:00:00'),
                       Timestamp('2016-03-25 00:00:00'),
                       Timestamp('2017-04-14 00:00:00'),
                       Timestamp('2018-03-30 00:00:00'),
                       Timestamp('2019-04-19 00:00:00'),
                       Timestamp('2020-04-10 00:00:00')]
        self.assertEqual(list(holidays), holidayList)
         datetime(2012, 5, 28),
         datetime(2013, 5, 27),
         datetime(2014, 5, 26),
         datetime(2015, 5, 25),
         datetime(2016, 5, 30),
         datetime(2017, 5, 29),
         datetime(2018, 5, 28),
         datetime(2019, 5, 27),
         datetime(2020, 5, 25),
     ],
 ),
 (
     Holiday("July 4th Eve", month=7, day=3),
     "2001-01-01",
     "2003-03-03",
     [Timestamp("2001-07-03 00:00:00"), Timestamp("2002-07-03 00:00:00")],
 ),
 (
     Holiday("July 4th Eve", month=7, day=3, days_of_week=(0, 1, 2, 3)),
     "2001-01-01",
     "2008-03-03",
     [
         Timestamp("2001-07-03 00:00:00"),
         Timestamp("2002-07-03 00:00:00"),
         Timestamp("2003-07-03 00:00:00"),
         Timestamp("2006-07-03 00:00:00"),
         Timestamp("2007-07-03 00:00:00"),
     ],
 ),
 (
     EasterMonday,
Example #18
0
from datetime import datetime

import pytest

from pandas import DatetimeIndex
import pandas.util.testing as tm

from pandas.tseries.holiday import (AbstractHolidayCalendar, Holiday,
                                    Timestamp, USFederalHolidayCalendar,
                                    USThanksgivingDay, get_calendar)


@pytest.mark.parametrize(
    "transform",
    [lambda x: x, lambda x: x.strftime("%Y-%m-%d"), lambda x: Timestamp(x)])
def test_calendar(transform):
    start_date = datetime(2012, 1, 1)
    end_date = datetime(2012, 12, 31)

    calendar = USFederalHolidayCalendar()
    holidays = calendar.holidays(transform(start_date), transform(end_date))

    expected = [
        datetime(2012, 1, 2),
        datetime(2012, 1, 16),
        datetime(2012, 2, 20),
        datetime(2012, 5, 28),
        datetime(2012, 7, 4),
        datetime(2012, 9, 3),
        datetime(2012, 10, 8),
        datetime(2012, 11, 12),
Example #19
0
         datetime(2012, 5, 28),
         datetime(2013, 5, 27),
         datetime(2014, 5, 26),
         datetime(2015, 5, 25),
         datetime(2016, 5, 30),
         datetime(2017, 5, 29),
         datetime(2018, 5, 28),
         datetime(2019, 5, 27),
         datetime(2020, 5, 25),
     ],
 ),
 (
     Holiday("July 4th Eve", month=7, day=3),
     "2001-01-01",
     "2003-03-03",
     [Timestamp("2001-07-03 00:00:00"), Timestamp("2002-07-03 00:00:00")],
 ),
 (
     Holiday("July 4th Eve", month=7, day=3, days_of_week=(0, 1, 2, 3)),
     "2001-01-01",
     "2008-03-03",
     [
         Timestamp("2001-07-03 00:00:00"),
         Timestamp("2002-07-03 00:00:00"),
         Timestamp("2003-07-03 00:00:00"),
         Timestamp("2006-07-03 00:00:00"),
         Timestamp("2007-07-03 00:00:00"),
     ],
 ),
 (
     EasterMonday,