{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Quickstart\n", "\n", "Everything starts with one `Calendar` instance.\n", "\n", "The calendar can be loaded by its name.\n", "\n", "Here the `ANBIMA` calendar is loaded." ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "Calendar: ANBIMA\n", "Start: 2000-01-01\n", "End: 2078-12-25\n", "Weekdays: Saturday, Sunday\n", "Holidays: 948\n", "Financial: True" ] }, "execution_count": 1, "metadata": {}, "output_type": "execute_result" } ], "source": [ "from bizdays import Calendar\n", "cal = Calendar.load(filename='ANBIMA.cal')\n", "cal" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Once a calendar is properly instanciated the methods can be directly called.\n", "\n", "Dates can be checked if they are business days or not with `isbizday`." ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "False" ] }, "execution_count": 2, "metadata": {}, "output_type": "execute_result" } ], "source": [ "cal.isbizday('2014-01-12')" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "cal.isbizday('2014-01-13')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Note that the dates are passed as strings with dates ISO formated.\n", "\n", "This is to simplify the use of bizdays.\n", "\n", "The methods accepts the following date types:\n", "\n", "- string ISO formated YYYY-MM-DD\n", "- `datetime.datetime`\n", "- `datetime.date`\n", "- `pandas.Timestamp`" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The amount of business days between two dates are obtained with the `bizdays` method." ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "253" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "cal.bizdays('2014-01-13', '2015-01-13')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "`following` and `preceding` methods always return business days.\n", "\n", "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.\n", "\n", "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.\n", "This usually happens when the bond has a standardised maturity as January first, for example." ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "datetime.date(2015, 12, 28)" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "cal.following('2015-12-25')" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "datetime.date(2015, 12, 28)" ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "cal.following('2015-12-28')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The `preceding` method does the oposite, it returns the previous business date or the passed date." ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "datetime.date(2013, 12, 31)" ] }, "execution_count": 7, "metadata": {}, "output_type": "execute_result" } ], "source": [ "cal.preceding('2014-01-01')" ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "datetime.date(2014, 1, 2)" ] }, "execution_count": 8, "metadata": {}, "output_type": "execute_result" } ], "source": [ "cal.preceding('2014-01-02')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "A sequence of business dates can be easily obtained with the `seq` method." ] }, { "cell_type": "code", "execution_count": 9, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[datetime.date(2014, 1, 2),\n", " datetime.date(2014, 1, 3),\n", " datetime.date(2014, 1, 6),\n", " datetime.date(2014, 1, 7)]" ] }, "execution_count": 9, "metadata": {}, "output_type": "execute_result" } ], "source": [ "cal.seq('2014-01-02', '2014-01-07')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Sometimes it is interesting to shift a date for a fixed amount of business days.\n", "\n", "The `offset` methods does that." ] }, { "cell_type": "code", "execution_count": 10, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "datetime.date(2014, 1, 9)" ] }, "execution_count": 10, "metadata": {}, "output_type": "execute_result" } ], "source": [ "cal.offset('2014-01-02', 5)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The `getdate` method uses the calendar to get general dates.\n", "\n", "For example, to get the 15th day of May 2020, just use:" ] }, { "cell_type": "code", "execution_count": 11, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "datetime.date(2002, 5, 15)" ] }, "execution_count": 11, "metadata": {}, "output_type": "execute_result" } ], "source": [ "cal.getdate('15th day', 2002, 5)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "If instead the desired date is the 15th business day of the same month, try:" ] }, { "cell_type": "code", "execution_count": 12, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "datetime.date(2002, 5, 22)" ] }, "execution_count": 12, "metadata": {}, "output_type": "execute_result" } ], "source": [ "cal.getdate('15th bizday', 2002, 5)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "It supports many other features like weekdays and some positional references." ] }, { "cell_type": "code", "execution_count": 13, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "datetime.date(2002, 5, 29)" ] }, "execution_count": 13, "metadata": {}, "output_type": "execute_result" } ], "source": [ "cal.getdate('last wed', 2002, 5)" ] }, { "cell_type": "code", "execution_count": 14, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "datetime.date(2002, 5, 24)" ] }, "execution_count": 14, "metadata": {}, "output_type": "execute_result" } ], "source": [ "cal.getdate('first fri before last day ', 2002, 5)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "If the desired information is the amount of business days for a specific year or month, the method `getbizdays` is recommended." ] }, { "cell_type": "code", "execution_count": 15, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "22" ] }, "execution_count": 15, "metadata": {}, "output_type": "execute_result" } ], "source": [ "cal.getbizdays(2001, 5)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "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." ] }, { "cell_type": "code", "execution_count": 16, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[2, 3]" ] }, "execution_count": 16, "metadata": {}, "output_type": "execute_result" } ], "source": [ "dates = ('2017-05-10', '2017-05-12', '2017-05-17')\n", "cal.diff(dates)" ] } ], "metadata": { "interpreter": { "hash": "4e6da9785688dca07ac236f8ec5f7e99c2c01a8eecf30ede8ddadf573b26e527" }, "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.7.6" } }, "nbformat": 4, "nbformat_minor": 2 }