Skip to main content

A calendar wrapper

Project description


Managing irregular calendars isn't hard per-se, but it can be painful, especially in the context of trading calendars and backtesting. That is why gregorian exposes a nifty Calendar class to work with custom calendars.


The best - and easiest - way to install Gregorian is by calling:

pip install gregorian 


import datetime
from gregorian import Calendar, utils

#every day in 2019
#you can load up your own dates from a backend, API, file...
calendar = Calendar([,1,1) + datetime.timedelta(i) for i in range(365)])

weekends = calendar.weekends()

#week days
weekdays = calendar.weekdays()

#first and last date
weekdays.first(), weekdays.last()

#or index
weekdays[0], weekdays[-1]

#slice the calendar
#like normal lists, the Calendar is 0-based, such that slice(10:20) returns the 11th to 20th dates

#or with a date-like object
#this will be inclusive of the bounds, if they are in the calendar
s2 = weekdays[,7,1),12,31)]

#10th weekday each month
b10 = weekdays.groupby("month").apply(lambda cal: cal[10])

#5th to 10th weekday each month
b5to10 = weekdays.groupby("month").apply(lambda cal: cal[5:10])

#lets retrieve holidays from somewhere (API, file...)
holidays = [, 1, 1),, 1, 21),, 2, 18),, 5, 27),, 7, 4),, 9, 2),, 10, 14),, 11, 11),, 11, 28),, 12, 25)

#because the calendar class is a sorted set, you have set methods like union, intersection and difference
#business days
workdays = weekdays.difference(holidays)

#... alternatively
workdays = holidays.inverse(, 1, 1),, 12, 31)).weekdays()

#non-weekend holidays
offdays = weekdays.union(holidays)

#is my birthday a weekday?, 6, 17) in weekdays
>> True

#slicing is inclusive of the two bounds... summer workdays
summer = workdays[,6,21),9,20)]

#index of given date in given frequency
workdays.dayof(,8,14), "month")
>> 8

#a few utilities
utils.isleap(2016), utils.isleap(,6,17))
>> True, False

#end of month
utils.eom(,2,14), 0), utils.eom(,2,14), -1)
>>, 2, 29),, 1, 31)

#... number of work days to new Year's eve
#week end, month end, quarter end, semester end or year end... simply ask for it!
workdays.daysto(to="year end",, 11, 15))
>> 45

About gregorian

This package is named after the Gregorian calendar, which was itself named after Pope Gregory XIII who introduced it as a correction to the Julian calendar in 1582.

Project details

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Files for gregorian, version 0.0.9
Filename, size File type Python version Upload date Hashes
Filename, size gregorian-0.0.9-py3-none-any.whl (11.0 kB) File type Wheel Python version py3 Upload date Hashes View
Filename, size gregorian-0.0.9.tar.gz (10.6 kB) File type Source Python version None Upload date Hashes View

Supported by

AWS AWS Cloud computing Datadog Datadog Monitoring DigiCert DigiCert EV certificate Facebook / Instagram Facebook / Instagram PSF Sponsor Fastly Fastly CDN Google Google Object Storage and Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Salesforce Salesforce PSF Sponsor Sentry Sentry Error logging StatusPage StatusPage Status page