@@ -546,6 +546,17 @@ calendars which account for local holidays and local weekend conventions.
546
546
print (dts)
547
547
print (Series(dts.weekday, dts).map(Series(' Mon Tue Wed Thu Fri Sat Sun' .split())))
548
548
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
+
549
560
.. note ::
550
561
551
562
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
712
723
``ms `` versus the new offset alias for month start ``MS ``. This means that
713
724
offset alias parsing is case sensitive.
714
725
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
+
715
793
.. _timeseries.advanced_datetime :
716
794
717
795
Time series-related instance methods
0 commit comments