Skip to content

Commit 8e3bdfe

Browse files
rockgjreback
authored andcommitted
ENH: Implementation of holidays and holiday calendars to be used with the
CustomBusinessDay offset. Also add an Easter holiday for use in calendars. HolidayCalendar changes (class factory, get_calendar, __repr__, and merge functions). HolidayCalendar start_date/end_date now public. Python3 changes and add additional tests for functionality. Also, add release note.
1 parent ee1923c commit 8e3bdfe

File tree

7 files changed

+663
-6
lines changed

7 files changed

+663
-6
lines changed

doc/source/release.rst

+1
Original file line numberDiff line numberDiff line change
@@ -219,6 +219,7 @@ Improvements to existing features
219219
- Performance improvements in timedelta conversions for integer dtypes (:issue:`6754`)
220220
- Performance improvement for ``DataFrame.from_records`` when reading a
221221
specified number of rows from an iterable (:issue:`6700`)
222+
- :ref:`Holidays and holiday calendars<timeseries.holiday>` are now available and can be used with CustomBusinessDay (:issue:`6719`)
222223

223224
.. _release.bug_fixes-0.14.0:
224225

doc/source/timeseries.rst

+78
Original file line numberDiff line numberDiff line change
@@ -546,6 +546,17 @@ calendars which account for local holidays and local weekend conventions.
546546
print(dts)
547547
print(Series(dts.weekday, dts).map(Series('Mon Tue Wed Thu Fri Sat Sun'.split())))
548548
549+
As of v0.14 holiday calendars can be used to provide the list of holidays. See the
550+
:ref:`holiday calendar<timeseries.holiday>` section for more information.
551+
552+
.. ipython:: python
553+
554+
from pandas.tseries.holiday import USFederalHolidayCalendar
555+
bday_us = CustomBusinessDay(calendar=USFederalHolidayCalendar())
556+
dt = datetime(2014, 1, 17) #Friday before MLK Day
557+
print(dt + bday_us) #Tuesday after MLK Day
558+
559+
549560
.. note::
550561

551562
The frequency string 'C' is used to indicate that a CustomBusinessDay
@@ -712,6 +723,73 @@ and business year ends. Please also note the legacy time rule for milliseconds
712723
``ms`` versus the new offset alias for month start ``MS``. This means that
713724
offset alias parsing is case sensitive.
714725

726+
.. _timeseries.holiday:
727+
728+
Holidays / Holiday Calendars
729+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~
730+
731+
Holidays and calendars provide a simple way to define holiday rules to be used
732+
with ``CustomBusinessDay`` or in other analysis that requires a predefined
733+
set of holidays. The ``AbstractHolidayCalendar`` class provides all the necessary
734+
methods to return a list of holidays and only ``rules`` need to be defined
735+
in a specific holiday calendar class. Further, ``start_date`` and ``end_date``
736+
class attributes determine over what date range holidays are generated. These
737+
should be overwritten on the ``AbstractHolidayCalendar`` class to have the range
738+
apply to all calendar subclasses. ``USFederalHolidayCalendar`` is the
739+
only calendar that exists and primarily serves as an example for developing
740+
other calendars.
741+
742+
For holidays that occur on fixed dates (e.g., US Memorial Day or July 4th) an
743+
observance rule determines when that holiday is observed if it falls on a weekend
744+
or some other non-observed day. Defined observance rules are:
745+
746+
.. csv-table::
747+
:header: "Rule", "Description"
748+
:widths: 15, 70
749+
750+
"nearest_workday", "move Saturday to Friday and Sunday to Monday"
751+
"sunday_to_monday", "move Sunday to following Monday"
752+
"next_monday_or_tuesday", "move Saturday to Monday and Sunday/Monday to Tuesday"
753+
"previous_friday", move Saturday and Sunday to previous Friday"
754+
"next_monday", "move Saturday and Sunday to following Monday"
755+
756+
An example of how holidays and holiday calendars are defined:
757+
758+
.. ipython:: python
759+
760+
from pandas.tseries.holiday import Holiday, USMemorialDay,\
761+
AbstractHolidayCalendar, nearest_workday, MO
762+
class ExampleCalendar(AbstractHolidayCalendar):
763+
rules = [
764+
USMemorialDay,
765+
Holiday('July 4th', month=7, day=4, observance=nearest_workday),
766+
Holiday('Columbus Day', month=10, day=1,
767+
offset=DateOffset(weekday=MO(2))), #same as 2*Week(weekday=2)
768+
]
769+
cal = ExampleCalendar()
770+
datetime(2012, 5, 25) + CustomBusinessDay(calendar=cal)
771+
cal.holidays(datetime(2012, 1, 1), datetime(2012, 12, 31))#holiday list
772+
AbstractHolidayCalendar.start_date #default start date of range
773+
AbstractHolidayCalendar.end_date #default end date of range
774+
AbstractHolidayCalendar.start_date = datetime(2012, 1, 1)#or Timestamp
775+
AbstractHolidayCalendar.end_date = datetime(2012, 12, 31)#or Timestamp
776+
cal.holidays()
777+
778+
Every calendar class is accessible by name using the ``get_calendar`` function
779+
which returns a holiday class instance. Any imported calendar class will
780+
automatically be available by this function. Also, ``HolidayCalendarFactory``
781+
provides an easy interface to create calendars that are combinations of calendars
782+
or calendars with additional rules.
783+
784+
.. ipython:: python
785+
786+
from pandas.tseries.holiday import get_calendar, HolidayCalendarFactory,\
787+
USLaborDay
788+
cal = get_calendar('ExampleCalendar')
789+
cal.rules
790+
new_cal = HolidayCalendarFactory('NewExampleCalendar', cal, USLaborDay)
791+
new_cal.rules
792+
715793
.. _timeseries.advanced_datetime:
716794

717795
Time series-related instance methods

doc/source/v0.14.0.txt

+3-1
Original file line numberDiff line numberDiff line change
@@ -325,7 +325,7 @@ Asymmetrical error bars are also supported, however raw error values must be pro
325325
Prior Version Deprecations/Changes
326326
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
327327

328-
Therse are prior version deprecations that are taking effect as of 0.14.0.
328+
There are prior version deprecations that are taking effect as of 0.14.0.
329329

330330
- Remove ``column`` keyword from ``DataFrame.sort`` (:issue:`4370`)
331331

@@ -391,6 +391,8 @@ Enhancements
391391
and data_label which allow the time stamp and dataset label to be set when creating a
392392
file. (:issue:`6545`)
393393
- ``pandas.io.gbq`` now handles reading unicode strings properly. (:issue:`5940`)
394+
- Improve performance of ``CustomBusinessDay`` (:issue:`6584`)
395+
- :ref:`Holidays and holiday calendars<timeseries.holiday>` are now available and can be used with CustomBusinessDay (:issue:`6719`)
394396

395397
Performance
396398
~~~~~~~~~~~

0 commit comments

Comments
 (0)