{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# pandas Integration\n", "\n", "`bizdays` has a very handy integration with pandas `DataFrame`." ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "from bizdays import set_option, Calendar\n", "\n", "set_option('mode', 'pandas')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "When the option `mode` is set to `pandas` the calendar methods will return\n", "\n", "- `Timestamp` instead of `datetime.date`\n", "- `DatetimeIndex` instead of lists of dates\n", "- `numpy.ndarray` instead of lists of integers (`bizdays`) and bools (`isbizday`)" ] }, { "cell_type": "code", "execution_count": 2, "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": 2, "metadata": {}, "output_type": "execute_result" } ], "source": [ "cal = Calendar.load(filename='ANBIMA.cal')\n", "cal" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([False, True])" ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "cal.isbizday(['2014-01-12', '2014-01-13'])" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([253, 503, ], dtype=object)" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "cal.bizdays('2014-01-13', ['2015-01-13', '2016-01-13', None])" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "Timestamp('2015-12-28 00:00:00')" ] }, "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": [ "DatetimeIndex(['2015-12-28', '2015-12-28'], dtype='datetime64[ns]', freq=None)" ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "cal.following(['2015-12-28', '2015-12-25'])" ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "DatetimeIndex(['2014-01-02', '2014-01-03', '2014-01-06', '2014-01-07'], dtype='datetime64[ns]', freq=None)" ] }, "execution_count": 7, "metadata": {}, "output_type": "execute_result" } ], "source": [ "cal.seq('2014-01-02', '2014-01-07')" ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "DatetimeIndex(['2014-01-02', '2014-01-03', '2014-01-06', '2014-01-07',\n", " '2014-01-08', '2014-01-09', '2014-01-10', '2014-01-13',\n", " '2014-01-14', '2014-01-15'],\n", " dtype='datetime64[ns]', freq=None)" ] }, "execution_count": 8, "metadata": {}, "output_type": "execute_result" } ], "source": [ "cal.offset('2014-01-02', range(10))" ] }, { "cell_type": "code", "execution_count": 9, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "Timestamp('2002-05-22 00:00:00')" ] }, "execution_count": 9, "metadata": {}, "output_type": "execute_result" } ], "source": [ "cal.getdate('15th bizday', 2002, 5)" ] }, { "cell_type": "code", "execution_count": 10, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "DatetimeIndex(['2002-01-15', '2002-02-15', '2002-03-15', '2002-04-15',\n", " '2002-05-15', '2002-06-15', '2002-07-15', '2002-08-15',\n", " '2002-09-15', '2002-10-15', '2002-11-15', '2002-12-15'],\n", " dtype='datetime64[ns]', freq=None)" ] }, "execution_count": 10, "metadata": {}, "output_type": "execute_result" } ], "source": [ "cal.getdate('15th day', 2002, range(1, 13))" ] }, { "cell_type": "code", "execution_count": 11, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([20, 18, 23, 20, 21, 21, 22, 22, 21, 20, 20, 23])" ] }, "execution_count": 11, "metadata": {}, "output_type": "execute_result" } ], "source": [ "cal.getbizdays(2021, range(1, 13))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Example: series of payments\n", "\n", "Let's create a series of payment dates and compute the business days between each payment date.\n", "\n", "The payments are scheduled to occur in the 15th days of each month of 2021.\n", "\n", "Start creating a DataFrame with year and month columns." ] }, { "cell_type": "code", "execution_count": 12, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
yearmonth
020211
120212
220213
320214
420215
520216
620217
720218
820219
9202110
10202111
11202112
\n", "
" ], "text/plain": [ " year month\n", "0 2021 1\n", "1 2021 2\n", "2 2021 3\n", "3 2021 4\n", "4 2021 5\n", "5 2021 6\n", "6 2021 7\n", "7 2021 8\n", "8 2021 9\n", "9 2021 10\n", "10 2021 11\n", "11 2021 12" ] }, "execution_count": 12, "metadata": {}, "output_type": "execute_result" } ], "source": [ "import pandas as pd\n", "\n", "df = pd.DataFrame({\n", " 'year': 2021,\n", " 'month': range(1,13)\n", "})\n", "df" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Use `getdate` to get the payment dates." ] }, { "cell_type": "code", "execution_count": 13, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
yearmonthpayment_dates
0202112021-01-15
1202122021-02-15
2202132021-03-15
3202142021-04-15
4202152021-05-15
5202162021-06-15
6202172021-07-15
7202182021-08-15
8202192021-09-15
92021102021-10-15
102021112021-11-15
112021122021-12-15
\n", "
" ], "text/plain": [ " year month payment_dates\n", "0 2021 1 2021-01-15\n", "1 2021 2 2021-02-15\n", "2 2021 3 2021-03-15\n", "3 2021 4 2021-04-15\n", "4 2021 5 2021-05-15\n", "5 2021 6 2021-06-15\n", "6 2021 7 2021-07-15\n", "7 2021 8 2021-08-15\n", "8 2021 9 2021-09-15\n", "9 2021 10 2021-10-15\n", "10 2021 11 2021-11-15\n", "11 2021 12 2021-12-15" ] }, "execution_count": 13, "metadata": {}, "output_type": "execute_result" } ], "source": [ "df['payment_dates'] = cal.getdate('15th day', df['year'], df['month'])\n", "df" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The payments happen in business days so the dates must be shifted to the next working date." ] }, { "cell_type": "code", "execution_count": 14, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
yearmonthpayment_datesfixing
0202112021-01-152021-01-15
1202122021-02-152021-02-17
2202132021-03-152021-03-15
3202142021-04-152021-04-15
4202152021-05-152021-05-17
5202162021-06-152021-06-15
6202172021-07-152021-07-15
7202182021-08-152021-08-16
8202192021-09-152021-09-15
92021102021-10-152021-10-15
102021112021-11-152021-11-16
112021122021-12-152021-12-15
\n", "
" ], "text/plain": [ " year month payment_dates fixing\n", "0 2021 1 2021-01-15 2021-01-15\n", "1 2021 2 2021-02-15 2021-02-17\n", "2 2021 3 2021-03-15 2021-03-15\n", "3 2021 4 2021-04-15 2021-04-15\n", "4 2021 5 2021-05-15 2021-05-17\n", "5 2021 6 2021-06-15 2021-06-15\n", "6 2021 7 2021-07-15 2021-07-15\n", "7 2021 8 2021-08-15 2021-08-16\n", "8 2021 9 2021-09-15 2021-09-15\n", "9 2021 10 2021-10-15 2021-10-15\n", "10 2021 11 2021-11-15 2021-11-16\n", "11 2021 12 2021-12-15 2021-12-15" ] }, "execution_count": 14, "metadata": {}, "output_type": "execute_result" } ], "source": [ "df['fixing'] = cal.following(df['payment_dates'])\n", "df" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "`bizdays` compute the amount of business days between each payment." ] }, { "cell_type": "code", "execution_count": 15, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
yearmonthpayment_datesfixingbusiness_days
0202112021-01-152021-01-15<NA>
1202122021-02-152021-02-1721
2202132021-03-152021-03-1518
3202142021-04-152021-04-1522
4202152021-05-152021-05-1721
5202162021-06-152021-06-1520
6202172021-07-152021-07-1522
7202182021-08-152021-08-1622
8202192021-09-152021-09-1521
92021102021-10-152021-10-1521
102021112021-11-152021-11-1620
112021122021-12-152021-12-1521
\n", "
" ], "text/plain": [ " year month payment_dates fixing business_days\n", "0 2021 1 2021-01-15 2021-01-15 \n", "1 2021 2 2021-02-15 2021-02-17 21\n", "2 2021 3 2021-03-15 2021-03-15 18\n", "3 2021 4 2021-04-15 2021-04-15 22\n", "4 2021 5 2021-05-15 2021-05-17 21\n", "5 2021 6 2021-06-15 2021-06-15 20\n", "6 2021 7 2021-07-15 2021-07-15 22\n", "7 2021 8 2021-08-15 2021-08-16 22\n", "8 2021 9 2021-09-15 2021-09-15 21\n", "9 2021 10 2021-10-15 2021-10-15 21\n", "10 2021 11 2021-11-15 2021-11-16 20\n", "11 2021 12 2021-12-15 2021-12-15 21" ] }, "execution_count": 15, "metadata": {}, "output_type": "execute_result" } ], "source": [ "df['business_days'] = cal.bizdays(df['fixing'].shift(), df['fixing'])\n", "df" ] } ], "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 }