Skip to content

Instantly share code, notes, and snippets.

@monkut
Created July 11, 2018 14:24
Show Gist options
  • Save monkut/07c0b4381ed70dc4527324706a6c21f4 to your computer and use it in GitHub Desktop.
Save monkut/07c0b4381ed70dc4527324706a6c21f4 to your computer and use it in GitHub Desktop.
Using pandas AbstractHolidayCalendar to calculate work days
import datetime
from pandas.tseries.holiday import AbstractHolidayCalendar, Holiday, sunday_to_monday, MO
from pandas.tseries.offsets import Day, CustomBusinessDay, DateOffset
class JapanBusinessCalendar(AbstractHolidayCalendar):
rules = [
Holiday('New Years Day', month=1, day=1),
Holiday('Coming of Age Day', month=1, day=8), # second monday of Jan
Holiday('National Foundation Day', month=2, day=11, observance=sunday_to_monday), # observed monday if falls on Sunday
Holiday('Vernal Equinox Day', month=3, day=21), # date of the holiday is not officially declared until February of the previous year, due to the need for recent astronomical measurements.
# golden week
# The days which are holidays each year depend on how the holidays fall in combination with the two weekends either side of Golden Week.
Holiday('Showa Day', month=4, day=29, observance=sunday_to_monday), # observance (unclear)
Holiday('Constitution Memorial day', month=5, day=3), # observance (unclear)
Holiday('Greenery Day', month=5, day=4), # observance (unclear)
Holiday('Childrens Day', month=5, day=4), # observance (unclear)
Holiday('Marine Day', month=7, day=16),
Holiday('Mountain Day', month=8, day=11, observance=sunday_to_monday), # observed monday if falls on Sunday
Holiday('Respect for the Aged Day', month=9, day=18, offset=DateOffset(weekday=MO(3))), # 3rd Monday of Sept
Holiday('Autumnal Equinox Day', month=9, day=23), # 23/24
Holiday('Health-Sports Day', month=10, day=9, offset=DateOffset(weekday=MO(2))), # 2nd Monday of October
Holiday('Culture Day', month=11, day=3, observance=sunday_to_monday), # observed monday if falls on Sunday
Holiday('Labour Day', month=11, day=23, observance=sunday_to_monday), # observed monday if falls on Sunday
Holiday('Emperors Birthday', month=12, day=23, observance=sunday_to_monday), # observed monday if falls on Sunday
]
JAPAN_BUSINESS_DAY_OFFSET = CustomBusinessDay(calendar=JapanBusinessCalendar())
# usage example
current = datetime.datetime(2018, 7, 11)
days_to_work = 15
work_dates = []
for i in range(days_to_work):
current += JAPAN_BUSINESS_DAY_OFFSET # changes to pandas timestamp object
work_dates.append(current.to_pydatetime())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment