Quickstart

Everything starts with one Calendar instance.

The calendar can be loaded by its name.

Here the ANBIMA calendar is loaded.

[1]:
from bizdays import Calendar
cal = Calendar.load(filename='ANBIMA.cal')
cal
[1]:
Calendar: ANBIMA
Start: 2000-01-01
End: 2078-12-25
Weekdays: Saturday, Sunday
Holidays: 948
Financial: True

Once a calendar is properly instanciated the methods can be directly called.

Dates can be checked if they are business days or not with isbizday.

[2]:
cal.isbizday('2014-01-12')
[2]:
False
[3]:
cal.isbizday('2014-01-13')
[3]:
True

Note that the dates are passed as strings with dates ISO formated.

This is to simplify the use of bizdays.

The methods accepts the following date types:

  • string ISO formated YYYY-MM-DD

  • datetime.datetime

  • datetime.date

  • pandas.Timestamp

The amount of business days between two dates are obtained with the bizdays method.

[4]:
cal.bizdays('2014-01-13', '2015-01-13')
[4]:
253

following and preceding methods always return business days.

The following method receives a date and if that date is a working date the same date is returned, otherwise, the next business date is returned.

This is very handy with calculations involving bonds and the maturity date, for example, is not a business day, and in this situation the maturity is shifted to the following business day. This usually happens when the bond has a standardised maturity as January first, for example.

[5]:
cal.following('2015-12-25')
[5]:
datetime.date(2015, 12, 28)
[6]:
cal.following('2015-12-28')
[6]:
datetime.date(2015, 12, 28)

The preceding method does the oposite, it returns the previous business date or the passed date.

[7]:
cal.preceding('2014-01-01')
[7]:
datetime.date(2013, 12, 31)
[8]:
cal.preceding('2014-01-02')
[8]:
datetime.date(2014, 1, 2)

A sequence of business dates can be easily obtained with the seq method.

[9]:
cal.seq('2014-01-02', '2014-01-07')
[9]:
[datetime.date(2014, 1, 2),
 datetime.date(2014, 1, 3),
 datetime.date(2014, 1, 6),
 datetime.date(2014, 1, 7)]

Sometimes it is interesting to shift a date for a fixed amount of business days.

The offset methods does that.

[10]:
cal.offset('2014-01-02', 5)
[10]:
datetime.date(2014, 1, 9)

The getdate method uses the calendar to get general dates.

For example, to get the 15th day of May 2020, just use:

[11]:
cal.getdate('15th day', 2002, 5)
[11]:
datetime.date(2002, 5, 15)

If instead the desired date is the 15th business day of the same month, try:

[12]:
cal.getdate('15th bizday', 2002, 5)
[12]:
datetime.date(2002, 5, 22)

It supports many other features like weekdays and some positional references.

[13]:
cal.getdate('last wed', 2002, 5)
[13]:
datetime.date(2002, 5, 29)
[14]:
cal.getdate('first fri before last day ', 2002, 5)
[14]:
datetime.date(2002, 5, 24)

If the desired information is the amount of business days for a specific year or month, the method getbizdays is recommended.

[15]:
cal.getbizdays(2001, 5)
[15]:
22

For a sequence of dates, the same way diff takes the difference between elements, Calendar.diff takes the difference in business days between given dates.

[16]:
dates = ('2017-05-10', '2017-05-12', '2017-05-17')
cal.diff(dates)
[16]:
[2, 3]