Skip to content

Instantly share code, notes, and snippets.

@vaclavdekanovsky
Last active October 3, 2020 20:05
Show Gist options
  • Select an option

  • Save vaclavdekanovsky/54db0f00be236590df0d6d764143a6d7 to your computer and use it in GitHub Desktop.

Select an option

Save vaclavdekanovsky/54db0f00be236590df0d6d764143a6d7 to your computer and use it in GitHub Desktop.
Using label and closed paramteres of the pandas upsample can lead to different groups when you use them on daily compared to the monthly resamples
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Different behavior of label and closed for daily and monthly resamples"
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
"import pandas as pd\n",
"import io"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Daily"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [],
"source": [
"data=\"\"\"\n",
"CAT|DATE|VALUE\n",
"abc|20200101|10\n",
"abc|20200103|20\n",
"abc|20200106|15\"\"\"\n",
"df = pd.read_csv(io.StringIO(data), skiprows=1, sep=\"|\", dtype={\"DATE\":\"str\"})\n",
"df[\"DATE\"] = pd.to_datetime(df[\"DATE\"], format=\"%Y%m%d\")"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"<style type=\"text/css\" >\n",
" #T_cb46542c_05b2_11eb_af9f_ab6e632dd49crow0_col2 {\n",
" background-color: lightgreen;\n",
" } #T_cb46542c_05b2_11eb_af9f_ab6e632dd49crow0_col5 {\n",
" background-color: lightgreen;\n",
" } #T_cb46542c_05b2_11eb_af9f_ab6e632dd49crow1_col2 {\n",
" background-color: lightgreen;\n",
" } #T_cb46542c_05b2_11eb_af9f_ab6e632dd49crow1_col5 {\n",
" background-color: lightgreen;\n",
" } #T_cb46542c_05b2_11eb_af9f_ab6e632dd49crow2_col2 {\n",
" background-color: lightgreen;\n",
" } #T_cb46542c_05b2_11eb_af9f_ab6e632dd49crow2_col5 {\n",
" background-color: lightgreen;\n",
" } #T_cb46542c_05b2_11eb_af9f_ab6e632dd49crow3_col2 {\n",
" background-color: lightgreen;\n",
" } #T_cb46542c_05b2_11eb_af9f_ab6e632dd49crow3_col5 {\n",
" background-color: lightgreen;\n",
" } #T_cb46542c_05b2_11eb_af9f_ab6e632dd49crow4_col2 {\n",
" background-color: lightgreen;\n",
" } #T_cb46542c_05b2_11eb_af9f_ab6e632dd49crow4_col5 {\n",
" background-color: lightgreen;\n",
" } #T_cb46542c_05b2_11eb_af9f_ab6e632dd49crow5_col2 {\n",
" background-color: lightgreen;\n",
" } #T_cb46542c_05b2_11eb_af9f_ab6e632dd49crow5_col5 {\n",
" background-color: lightgreen;\n",
" } #T_cb46542c_05b2_11eb_af9f_ab6e632dd49crow6_col2 {\n",
" background-color: lightgreen;\n",
" } #T_cb46542c_05b2_11eb_af9f_ab6e632dd49crow6_col5 {\n",
" background-color: lightgreen;\n",
" } #T_cb46542c_05b2_11eb_af9f_ab6e632dd49crow7_col2 {\n",
" background-color: lightgreen;\n",
" } #T_cb46542c_05b2_11eb_af9f_ab6e632dd49crow7_col5 {\n",
" background-color: lightgreen;\n",
" }</style><table id=\"T_cb46542c_05b2_11eb_af9f_ab6e632dd49c\" ><thead> <tr> <th class=\"blank level0\" ></th> <th class=\"col_heading level0 col0\" >l:left_c:left</th> <th class=\"col_heading level0 col1\" >l:left_c:right</th> <th class=\"col_heading level0 col2\" >l:right_c:left</th> <th class=\"col_heading level0 col3\" >l:right_c:right</th> <th class=\"col_heading level0 col4\" >l:left</th> <th class=\"col_heading level0 col5\" >l:right</th> </tr></thead><tbody>\n",
" <tr>\n",
" <th id=\"T_cb46542c_05b2_11eb_af9f_ab6e632dd49clevel0_row0\" class=\"row_heading level0 row0\" >2019-12-31 00:00:00</th>\n",
" <td id=\"T_cb46542c_05b2_11eb_af9f_ab6e632dd49crow0_col0\" class=\"data row0 col0\" >0</td>\n",
" <td id=\"T_cb46542c_05b2_11eb_af9f_ab6e632dd49crow0_col1\" class=\"data row0 col1\" >1</td>\n",
" <td id=\"T_cb46542c_05b2_11eb_af9f_ab6e632dd49crow0_col2\" class=\"data row0 col2\" >0</td>\n",
" <td id=\"T_cb46542c_05b2_11eb_af9f_ab6e632dd49crow0_col3\" class=\"data row0 col3\" >0</td>\n",
" <td id=\"T_cb46542c_05b2_11eb_af9f_ab6e632dd49crow0_col4\" class=\"data row0 col4\" >0</td>\n",
" <td id=\"T_cb46542c_05b2_11eb_af9f_ab6e632dd49crow0_col5\" class=\"data row0 col5\" >0</td>\n",
" </tr>\n",
" <tr>\n",
" <th id=\"T_cb46542c_05b2_11eb_af9f_ab6e632dd49clevel0_row1\" class=\"row_heading level0 row1\" >2020-01-01 00:00:00</th>\n",
" <td id=\"T_cb46542c_05b2_11eb_af9f_ab6e632dd49crow1_col0\" class=\"data row1 col0\" >1</td>\n",
" <td id=\"T_cb46542c_05b2_11eb_af9f_ab6e632dd49crow1_col1\" class=\"data row1 col1\" >1</td>\n",
" <td id=\"T_cb46542c_05b2_11eb_af9f_ab6e632dd49crow1_col2\" class=\"data row1 col2\" >0</td>\n",
" <td id=\"T_cb46542c_05b2_11eb_af9f_ab6e632dd49crow1_col3\" class=\"data row1 col3\" >1</td>\n",
" <td id=\"T_cb46542c_05b2_11eb_af9f_ab6e632dd49crow1_col4\" class=\"data row1 col4\" >1</td>\n",
" <td id=\"T_cb46542c_05b2_11eb_af9f_ab6e632dd49crow1_col5\" class=\"data row1 col5\" >0</td>\n",
" </tr>\n",
" <tr>\n",
" <th id=\"T_cb46542c_05b2_11eb_af9f_ab6e632dd49clevel0_row2\" class=\"row_heading level0 row2\" >2020-01-02 00:00:00</th>\n",
" <td id=\"T_cb46542c_05b2_11eb_af9f_ab6e632dd49crow2_col0\" class=\"data row2 col0\" >1</td>\n",
" <td id=\"T_cb46542c_05b2_11eb_af9f_ab6e632dd49crow2_col1\" class=\"data row2 col1\" >2</td>\n",
" <td id=\"T_cb46542c_05b2_11eb_af9f_ab6e632dd49crow2_col2\" class=\"data row2 col2\" >1</td>\n",
" <td id=\"T_cb46542c_05b2_11eb_af9f_ab6e632dd49crow2_col3\" class=\"data row2 col3\" >1</td>\n",
" <td id=\"T_cb46542c_05b2_11eb_af9f_ab6e632dd49crow2_col4\" class=\"data row2 col4\" >1</td>\n",
" <td id=\"T_cb46542c_05b2_11eb_af9f_ab6e632dd49crow2_col5\" class=\"data row2 col5\" >1</td>\n",
" </tr>\n",
" <tr>\n",
" <th id=\"T_cb46542c_05b2_11eb_af9f_ab6e632dd49clevel0_row3\" class=\"row_heading level0 row3\" >2020-01-03 00:00:00</th>\n",
" <td id=\"T_cb46542c_05b2_11eb_af9f_ab6e632dd49crow3_col0\" class=\"data row3 col0\" >2</td>\n",
" <td id=\"T_cb46542c_05b2_11eb_af9f_ab6e632dd49crow3_col1\" class=\"data row3 col1\" >2</td>\n",
" <td id=\"T_cb46542c_05b2_11eb_af9f_ab6e632dd49crow3_col2\" class=\"data row3 col2\" >1</td>\n",
" <td id=\"T_cb46542c_05b2_11eb_af9f_ab6e632dd49crow3_col3\" class=\"data row3 col3\" >2</td>\n",
" <td id=\"T_cb46542c_05b2_11eb_af9f_ab6e632dd49crow3_col4\" class=\"data row3 col4\" >2</td>\n",
" <td id=\"T_cb46542c_05b2_11eb_af9f_ab6e632dd49crow3_col5\" class=\"data row3 col5\" >1</td>\n",
" </tr>\n",
" <tr>\n",
" <th id=\"T_cb46542c_05b2_11eb_af9f_ab6e632dd49clevel0_row4\" class=\"row_heading level0 row4\" >2020-01-04 00:00:00</th>\n",
" <td id=\"T_cb46542c_05b2_11eb_af9f_ab6e632dd49crow4_col0\" class=\"data row4 col0\" >2</td>\n",
" <td id=\"T_cb46542c_05b2_11eb_af9f_ab6e632dd49crow4_col1\" class=\"data row4 col1\" >2</td>\n",
" <td id=\"T_cb46542c_05b2_11eb_af9f_ab6e632dd49crow4_col2\" class=\"data row4 col2\" >2</td>\n",
" <td id=\"T_cb46542c_05b2_11eb_af9f_ab6e632dd49crow4_col3\" class=\"data row4 col3\" >2</td>\n",
" <td id=\"T_cb46542c_05b2_11eb_af9f_ab6e632dd49crow4_col4\" class=\"data row4 col4\" >2</td>\n",
" <td id=\"T_cb46542c_05b2_11eb_af9f_ab6e632dd49crow4_col5\" class=\"data row4 col5\" >2</td>\n",
" </tr>\n",
" <tr>\n",
" <th id=\"T_cb46542c_05b2_11eb_af9f_ab6e632dd49clevel0_row5\" class=\"row_heading level0 row5\" >2020-01-05 00:00:00</th>\n",
" <td id=\"T_cb46542c_05b2_11eb_af9f_ab6e632dd49crow5_col0\" class=\"data row5 col0\" >2</td>\n",
" <td id=\"T_cb46542c_05b2_11eb_af9f_ab6e632dd49crow5_col1\" class=\"data row5 col1\" >3</td>\n",
" <td id=\"T_cb46542c_05b2_11eb_af9f_ab6e632dd49crow5_col2\" class=\"data row5 col2\" >2</td>\n",
" <td id=\"T_cb46542c_05b2_11eb_af9f_ab6e632dd49crow5_col3\" class=\"data row5 col3\" >2</td>\n",
" <td id=\"T_cb46542c_05b2_11eb_af9f_ab6e632dd49crow5_col4\" class=\"data row5 col4\" >2</td>\n",
" <td id=\"T_cb46542c_05b2_11eb_af9f_ab6e632dd49crow5_col5\" class=\"data row5 col5\" >2</td>\n",
" </tr>\n",
" <tr>\n",
" <th id=\"T_cb46542c_05b2_11eb_af9f_ab6e632dd49clevel0_row6\" class=\"row_heading level0 row6\" >2020-01-06 00:00:00</th>\n",
" <td id=\"T_cb46542c_05b2_11eb_af9f_ab6e632dd49crow6_col0\" class=\"data row6 col0\" >3</td>\n",
" <td id=\"T_cb46542c_05b2_11eb_af9f_ab6e632dd49crow6_col1\" class=\"data row6 col1\" >0</td>\n",
" <td id=\"T_cb46542c_05b2_11eb_af9f_ab6e632dd49crow6_col2\" class=\"data row6 col2\" >2</td>\n",
" <td id=\"T_cb46542c_05b2_11eb_af9f_ab6e632dd49crow6_col3\" class=\"data row6 col3\" >3</td>\n",
" <td id=\"T_cb46542c_05b2_11eb_af9f_ab6e632dd49crow6_col4\" class=\"data row6 col4\" >3</td>\n",
" <td id=\"T_cb46542c_05b2_11eb_af9f_ab6e632dd49crow6_col5\" class=\"data row6 col5\" >2</td>\n",
" </tr>\n",
" <tr>\n",
" <th id=\"T_cb46542c_05b2_11eb_af9f_ab6e632dd49clevel0_row7\" class=\"row_heading level0 row7\" >2020-01-07 00:00:00</th>\n",
" <td id=\"T_cb46542c_05b2_11eb_af9f_ab6e632dd49crow7_col0\" class=\"data row7 col0\" >0</td>\n",
" <td id=\"T_cb46542c_05b2_11eb_af9f_ab6e632dd49crow7_col1\" class=\"data row7 col1\" >0</td>\n",
" <td id=\"T_cb46542c_05b2_11eb_af9f_ab6e632dd49crow7_col2\" class=\"data row7 col2\" >3</td>\n",
" <td id=\"T_cb46542c_05b2_11eb_af9f_ab6e632dd49crow7_col3\" class=\"data row7 col3\" >0</td>\n",
" <td id=\"T_cb46542c_05b2_11eb_af9f_ab6e632dd49crow7_col4\" class=\"data row7 col4\" >0</td>\n",
" <td id=\"T_cb46542c_05b2_11eb_af9f_ab6e632dd49crow7_col5\" class=\"data row7 col5\" >3</td>\n",
" </tr>\n",
" </tbody></table>"
],
"text/plain": [
"<pandas.io.formats.style.Styler at 0x7f6de04ee2e0>"
]
},
"execution_count": 3,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"groups = []\n",
"for label in [\"left\",\"right\"]:\n",
" for closed in [\"left\",\"right\"]:\n",
" resampled_groups = df.set_index(\"DATE\").resample(\"D\", label=label, closed=closed).groups\n",
" groups.append(pd.DataFrame(resampled_groups, index=[\"l:\"+label+\"_c:\"+closed]).T)\n",
"\n",
"for label in [\"left\",\"right\"]:\n",
" resampled_groups = df.set_index(\"DATE\").resample(\"D\", label=label).groups\n",
" groups.append(pd.DataFrame(resampled_groups, index=[\"l:\"+label]).T)\n",
" \n",
"out = pd.concat(groups, axis=1).fillna(\"0\").astype(\"int\")\n",
"\n",
"def highlight_cols(s):\n",
" return 'background-color: lightgreen'\n",
"\n",
"out.style.applymap(highlight_cols, subset=[\"l:right_c:left\",\"l:right\"])"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Monthly"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [],
"source": [
"data=\"\"\"\n",
"COM|DATE |VALUE\n",
"efg|20200331|25\n",
"efg|20200930|75\n",
"efg|20201031|15\"\"\"\n",
"\n",
"# read the data into dataframe\n",
"df = pd.read_csv(io.StringIO(data), skiprows=1, sep=\"|\", dtype={\"DATE \":\"str\"}).rename(columns={\"DATE \":\"DATE\"})\n",
"df[\"DATE\"] = pd.to_datetime(df[\"DATE\"], format=\"%Y%m%d\")"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"<style type=\"text/css\" >\n",
" #T_dc3b67e0_05b2_11eb_af9f_ab6e632dd49crow0_col3 {\n",
" background-color: lightgreen;\n",
" } #T_dc3b67e0_05b2_11eb_af9f_ab6e632dd49crow0_col5 {\n",
" background-color: lightgreen;\n",
" } #T_dc3b67e0_05b2_11eb_af9f_ab6e632dd49crow1_col3 {\n",
" background-color: lightgreen;\n",
" } #T_dc3b67e0_05b2_11eb_af9f_ab6e632dd49crow1_col5 {\n",
" background-color: lightgreen;\n",
" } #T_dc3b67e0_05b2_11eb_af9f_ab6e632dd49crow2_col3 {\n",
" background-color: lightgreen;\n",
" } #T_dc3b67e0_05b2_11eb_af9f_ab6e632dd49crow2_col5 {\n",
" background-color: lightgreen;\n",
" } #T_dc3b67e0_05b2_11eb_af9f_ab6e632dd49crow3_col3 {\n",
" background-color: lightgreen;\n",
" } #T_dc3b67e0_05b2_11eb_af9f_ab6e632dd49crow3_col5 {\n",
" background-color: lightgreen;\n",
" } #T_dc3b67e0_05b2_11eb_af9f_ab6e632dd49crow4_col3 {\n",
" background-color: lightgreen;\n",
" } #T_dc3b67e0_05b2_11eb_af9f_ab6e632dd49crow4_col5 {\n",
" background-color: lightgreen;\n",
" } #T_dc3b67e0_05b2_11eb_af9f_ab6e632dd49crow5_col3 {\n",
" background-color: lightgreen;\n",
" } #T_dc3b67e0_05b2_11eb_af9f_ab6e632dd49crow5_col5 {\n",
" background-color: lightgreen;\n",
" } #T_dc3b67e0_05b2_11eb_af9f_ab6e632dd49crow6_col3 {\n",
" background-color: lightgreen;\n",
" } #T_dc3b67e0_05b2_11eb_af9f_ab6e632dd49crow6_col5 {\n",
" background-color: lightgreen;\n",
" } #T_dc3b67e0_05b2_11eb_af9f_ab6e632dd49crow7_col3 {\n",
" background-color: lightgreen;\n",
" } #T_dc3b67e0_05b2_11eb_af9f_ab6e632dd49crow7_col5 {\n",
" background-color: lightgreen;\n",
" } #T_dc3b67e0_05b2_11eb_af9f_ab6e632dd49crow8_col3 {\n",
" background-color: lightgreen;\n",
" } #T_dc3b67e0_05b2_11eb_af9f_ab6e632dd49crow8_col5 {\n",
" background-color: lightgreen;\n",
" } #T_dc3b67e0_05b2_11eb_af9f_ab6e632dd49crow9_col3 {\n",
" background-color: lightgreen;\n",
" } #T_dc3b67e0_05b2_11eb_af9f_ab6e632dd49crow9_col5 {\n",
" background-color: lightgreen;\n",
" }</style><table id=\"T_dc3b67e0_05b2_11eb_af9f_ab6e632dd49c\" ><thead> <tr> <th class=\"blank level0\" ></th> <th class=\"col_heading level0 col0\" >l:left_left</th> <th class=\"col_heading level0 col1\" >l:left_right</th> <th class=\"col_heading level0 col2\" >l:right_left</th> <th class=\"col_heading level0 col3\" >l:right_right</th> <th class=\"col_heading level0 col4\" >l:left</th> <th class=\"col_heading level0 col5\" >l:right</th> </tr></thead><tbody>\n",
" <tr>\n",
" <th id=\"T_dc3b67e0_05b2_11eb_af9f_ab6e632dd49clevel0_row0\" class=\"row_heading level0 row0\" >2020-02-29 00:00:00</th>\n",
" <td id=\"T_dc3b67e0_05b2_11eb_af9f_ab6e632dd49crow0_col0\" class=\"data row0 col0\" >0</td>\n",
" <td id=\"T_dc3b67e0_05b2_11eb_af9f_ab6e632dd49crow0_col1\" class=\"data row0 col1\" >1</td>\n",
" <td id=\"T_dc3b67e0_05b2_11eb_af9f_ab6e632dd49crow0_col2\" class=\"data row0 col2\" >0</td>\n",
" <td id=\"T_dc3b67e0_05b2_11eb_af9f_ab6e632dd49crow0_col3\" class=\"data row0 col3\" >0</td>\n",
" <td id=\"T_dc3b67e0_05b2_11eb_af9f_ab6e632dd49crow0_col4\" class=\"data row0 col4\" >1</td>\n",
" <td id=\"T_dc3b67e0_05b2_11eb_af9f_ab6e632dd49crow0_col5\" class=\"data row0 col5\" >0</td>\n",
" </tr>\n",
" <tr>\n",
" <th id=\"T_dc3b67e0_05b2_11eb_af9f_ab6e632dd49clevel0_row1\" class=\"row_heading level0 row1\" >2020-03-31 00:00:00</th>\n",
" <td id=\"T_dc3b67e0_05b2_11eb_af9f_ab6e632dd49crow1_col0\" class=\"data row1 col0\" >1</td>\n",
" <td id=\"T_dc3b67e0_05b2_11eb_af9f_ab6e632dd49crow1_col1\" class=\"data row1 col1\" >1</td>\n",
" <td id=\"T_dc3b67e0_05b2_11eb_af9f_ab6e632dd49crow1_col2\" class=\"data row1 col2\" >0</td>\n",
" <td id=\"T_dc3b67e0_05b2_11eb_af9f_ab6e632dd49crow1_col3\" class=\"data row1 col3\" >1</td>\n",
" <td id=\"T_dc3b67e0_05b2_11eb_af9f_ab6e632dd49crow1_col4\" class=\"data row1 col4\" >1</td>\n",
" <td id=\"T_dc3b67e0_05b2_11eb_af9f_ab6e632dd49crow1_col5\" class=\"data row1 col5\" >1</td>\n",
" </tr>\n",
" <tr>\n",
" <th id=\"T_dc3b67e0_05b2_11eb_af9f_ab6e632dd49clevel0_row2\" class=\"row_heading level0 row2\" >2020-04-30 00:00:00</th>\n",
" <td id=\"T_dc3b67e0_05b2_11eb_af9f_ab6e632dd49crow2_col0\" class=\"data row2 col0\" >1</td>\n",
" <td id=\"T_dc3b67e0_05b2_11eb_af9f_ab6e632dd49crow2_col1\" class=\"data row2 col1\" >1</td>\n",
" <td id=\"T_dc3b67e0_05b2_11eb_af9f_ab6e632dd49crow2_col2\" class=\"data row2 col2\" >1</td>\n",
" <td id=\"T_dc3b67e0_05b2_11eb_af9f_ab6e632dd49crow2_col3\" class=\"data row2 col3\" >1</td>\n",
" <td id=\"T_dc3b67e0_05b2_11eb_af9f_ab6e632dd49crow2_col4\" class=\"data row2 col4\" >1</td>\n",
" <td id=\"T_dc3b67e0_05b2_11eb_af9f_ab6e632dd49crow2_col5\" class=\"data row2 col5\" >1</td>\n",
" </tr>\n",
" <tr>\n",
" <th id=\"T_dc3b67e0_05b2_11eb_af9f_ab6e632dd49clevel0_row3\" class=\"row_heading level0 row3\" >2020-05-31 00:00:00</th>\n",
" <td id=\"T_dc3b67e0_05b2_11eb_af9f_ab6e632dd49crow3_col0\" class=\"data row3 col0\" >1</td>\n",
" <td id=\"T_dc3b67e0_05b2_11eb_af9f_ab6e632dd49crow3_col1\" class=\"data row3 col1\" >1</td>\n",
" <td id=\"T_dc3b67e0_05b2_11eb_af9f_ab6e632dd49crow3_col2\" class=\"data row3 col2\" >1</td>\n",
" <td id=\"T_dc3b67e0_05b2_11eb_af9f_ab6e632dd49crow3_col3\" class=\"data row3 col3\" >1</td>\n",
" <td id=\"T_dc3b67e0_05b2_11eb_af9f_ab6e632dd49crow3_col4\" class=\"data row3 col4\" >1</td>\n",
" <td id=\"T_dc3b67e0_05b2_11eb_af9f_ab6e632dd49crow3_col5\" class=\"data row3 col5\" >1</td>\n",
" </tr>\n",
" <tr>\n",
" <th id=\"T_dc3b67e0_05b2_11eb_af9f_ab6e632dd49clevel0_row4\" class=\"row_heading level0 row4\" >2020-06-30 00:00:00</th>\n",
" <td id=\"T_dc3b67e0_05b2_11eb_af9f_ab6e632dd49crow4_col0\" class=\"data row4 col0\" >1</td>\n",
" <td id=\"T_dc3b67e0_05b2_11eb_af9f_ab6e632dd49crow4_col1\" class=\"data row4 col1\" >1</td>\n",
" <td id=\"T_dc3b67e0_05b2_11eb_af9f_ab6e632dd49crow4_col2\" class=\"data row4 col2\" >1</td>\n",
" <td id=\"T_dc3b67e0_05b2_11eb_af9f_ab6e632dd49crow4_col3\" class=\"data row4 col3\" >1</td>\n",
" <td id=\"T_dc3b67e0_05b2_11eb_af9f_ab6e632dd49crow4_col4\" class=\"data row4 col4\" >1</td>\n",
" <td id=\"T_dc3b67e0_05b2_11eb_af9f_ab6e632dd49crow4_col5\" class=\"data row4 col5\" >1</td>\n",
" </tr>\n",
" <tr>\n",
" <th id=\"T_dc3b67e0_05b2_11eb_af9f_ab6e632dd49clevel0_row5\" class=\"row_heading level0 row5\" >2020-07-31 00:00:00</th>\n",
" <td id=\"T_dc3b67e0_05b2_11eb_af9f_ab6e632dd49crow5_col0\" class=\"data row5 col0\" >1</td>\n",
" <td id=\"T_dc3b67e0_05b2_11eb_af9f_ab6e632dd49crow5_col1\" class=\"data row5 col1\" >1</td>\n",
" <td id=\"T_dc3b67e0_05b2_11eb_af9f_ab6e632dd49crow5_col2\" class=\"data row5 col2\" >1</td>\n",
" <td id=\"T_dc3b67e0_05b2_11eb_af9f_ab6e632dd49crow5_col3\" class=\"data row5 col3\" >1</td>\n",
" <td id=\"T_dc3b67e0_05b2_11eb_af9f_ab6e632dd49crow5_col4\" class=\"data row5 col4\" >1</td>\n",
" <td id=\"T_dc3b67e0_05b2_11eb_af9f_ab6e632dd49crow5_col5\" class=\"data row5 col5\" >1</td>\n",
" </tr>\n",
" <tr>\n",
" <th id=\"T_dc3b67e0_05b2_11eb_af9f_ab6e632dd49clevel0_row6\" class=\"row_heading level0 row6\" >2020-08-31 00:00:00</th>\n",
" <td id=\"T_dc3b67e0_05b2_11eb_af9f_ab6e632dd49crow6_col0\" class=\"data row6 col0\" >1</td>\n",
" <td id=\"T_dc3b67e0_05b2_11eb_af9f_ab6e632dd49crow6_col1\" class=\"data row6 col1\" >2</td>\n",
" <td id=\"T_dc3b67e0_05b2_11eb_af9f_ab6e632dd49crow6_col2\" class=\"data row6 col2\" >1</td>\n",
" <td id=\"T_dc3b67e0_05b2_11eb_af9f_ab6e632dd49crow6_col3\" class=\"data row6 col3\" >1</td>\n",
" <td id=\"T_dc3b67e0_05b2_11eb_af9f_ab6e632dd49crow6_col4\" class=\"data row6 col4\" >2</td>\n",
" <td id=\"T_dc3b67e0_05b2_11eb_af9f_ab6e632dd49crow6_col5\" class=\"data row6 col5\" >1</td>\n",
" </tr>\n",
" <tr>\n",
" <th id=\"T_dc3b67e0_05b2_11eb_af9f_ab6e632dd49clevel0_row7\" class=\"row_heading level0 row7\" >2020-09-30 00:00:00</th>\n",
" <td id=\"T_dc3b67e0_05b2_11eb_af9f_ab6e632dd49crow7_col0\" class=\"data row7 col0\" >2</td>\n",
" <td id=\"T_dc3b67e0_05b2_11eb_af9f_ab6e632dd49crow7_col1\" class=\"data row7 col1\" >3</td>\n",
" <td id=\"T_dc3b67e0_05b2_11eb_af9f_ab6e632dd49crow7_col2\" class=\"data row7 col2\" >1</td>\n",
" <td id=\"T_dc3b67e0_05b2_11eb_af9f_ab6e632dd49crow7_col3\" class=\"data row7 col3\" >2</td>\n",
" <td id=\"T_dc3b67e0_05b2_11eb_af9f_ab6e632dd49crow7_col4\" class=\"data row7 col4\" >3</td>\n",
" <td id=\"T_dc3b67e0_05b2_11eb_af9f_ab6e632dd49crow7_col5\" class=\"data row7 col5\" >2</td>\n",
" </tr>\n",
" <tr>\n",
" <th id=\"T_dc3b67e0_05b2_11eb_af9f_ab6e632dd49clevel0_row8\" class=\"row_heading level0 row8\" >2020-10-31 00:00:00</th>\n",
" <td id=\"T_dc3b67e0_05b2_11eb_af9f_ab6e632dd49crow8_col0\" class=\"data row8 col0\" >3</td>\n",
" <td id=\"T_dc3b67e0_05b2_11eb_af9f_ab6e632dd49crow8_col1\" class=\"data row8 col1\" >0</td>\n",
" <td id=\"T_dc3b67e0_05b2_11eb_af9f_ab6e632dd49crow8_col2\" class=\"data row8 col2\" >2</td>\n",
" <td id=\"T_dc3b67e0_05b2_11eb_af9f_ab6e632dd49crow8_col3\" class=\"data row8 col3\" >3</td>\n",
" <td id=\"T_dc3b67e0_05b2_11eb_af9f_ab6e632dd49crow8_col4\" class=\"data row8 col4\" >0</td>\n",
" <td id=\"T_dc3b67e0_05b2_11eb_af9f_ab6e632dd49crow8_col5\" class=\"data row8 col5\" >3</td>\n",
" </tr>\n",
" <tr>\n",
" <th id=\"T_dc3b67e0_05b2_11eb_af9f_ab6e632dd49clevel0_row9\" class=\"row_heading level0 row9\" >2020-11-30 00:00:00</th>\n",
" <td id=\"T_dc3b67e0_05b2_11eb_af9f_ab6e632dd49crow9_col0\" class=\"data row9 col0\" >0</td>\n",
" <td id=\"T_dc3b67e0_05b2_11eb_af9f_ab6e632dd49crow9_col1\" class=\"data row9 col1\" >0</td>\n",
" <td id=\"T_dc3b67e0_05b2_11eb_af9f_ab6e632dd49crow9_col2\" class=\"data row9 col2\" >3</td>\n",
" <td id=\"T_dc3b67e0_05b2_11eb_af9f_ab6e632dd49crow9_col3\" class=\"data row9 col3\" >0</td>\n",
" <td id=\"T_dc3b67e0_05b2_11eb_af9f_ab6e632dd49crow9_col4\" class=\"data row9 col4\" >0</td>\n",
" <td id=\"T_dc3b67e0_05b2_11eb_af9f_ab6e632dd49crow9_col5\" class=\"data row9 col5\" >0</td>\n",
" </tr>\n",
" </tbody></table>"
],
"text/plain": [
"<pandas.io.formats.style.Styler at 0x7f6dc471db20>"
]
},
"execution_count": 5,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"groups = []\n",
"for label in [\"left\",\"right\"]:\n",
" for closed in [\"left\",\"right\"]:\n",
" resampled_groups = df.set_index(\"DATE\").resample(\"M\", label=label, closed=closed).groups\n",
" groups.append(pd.DataFrame(resampled_groups, index=[\"l:\"+label+\"_\"+closed]).T)\n",
"for label in [\"left\",\"right\"]:\n",
" resampled_groups = df.set_index(\"DATE\").resample(\"M\", label=label).groups\n",
" groups.append(pd.DataFrame(resampled_groups, index=[\"l:\"+label],dtype=\"int\").T)\n",
"out = pd.concat(groups, axis=1).fillna(\"0\").astype(\"int\")\n",
"\n",
"def highlight_cols(s):\n",
" return 'background-color: lightgreen'\n",
"\n",
"out.style.applymap(highlight_cols, subset=pd.IndexSlice[:,[\"l:right_right\",\"l:right\"]])"
]
}
],
"metadata": {
"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.8.3"
}
},
"nbformat": 4,
"nbformat_minor": 4
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment