-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
1,564 additions
and
1 deletion.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,286 @@ | ||
{ | ||
"cells": [ | ||
{ | ||
"cell_type": "markdown", | ||
"id": "e66d545b-5f7c-40cb-a396-892df7282631", | ||
"metadata": {}, | ||
"source": [ | ||
"# Snippets\n", | ||
"\n", | ||
"This is a compilation of useful code snippets.\n", | ||
"\n", | ||
"## Combine Overlapping Intervals in Pandas\n", | ||
"\n", | ||
"Suppose we have the start and end points of several intervals. How do we combine them if the intervals overlap?\n", | ||
"\n", | ||
"This solution is taken from https://stackoverflow.com/questions/57882621/efficient-merge-overlapping-intervals-in-same-pandas-dataframe-with-start-and-fi. Credit goes to users Dev Khadka and John Smith.\n", | ||
"\n", | ||
"Take the following example." | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 32, | ||
"id": "6d79c1e9-24bf-4f2b-9a04-e10461f8c816", | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"import pandas as pd\n", | ||
"\n", | ||
"intervals = pd.DataFrame({\n", | ||
" \"start_point\": [3, 5, 0, 10],\n", | ||
" \"end_point\": [8, 7, 1, 11],\n", | ||
"})" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"id": "f8bd71d2-d3f8-4a5e-a848-e7d838cd0a33", | ||
"metadata": {}, | ||
"source": [ | ||
"The $[3, 6]$ and $[5, 7]$ intervals should be combined into $[3, 7]$. We will solve this by grouping the rows together if thier intervals overlap, then we select the minimum start point and maximum end point in each group.\n", | ||
"\n", | ||
"Sort by the start point." | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 41, | ||
"id": "9e7631d9-6fa3-4e3e-b22a-22edf734afc0", | ||
"metadata": {}, | ||
"outputs": [ | ||
{ | ||
"data": { | ||
"text/html": [ | ||
"<div>\n", | ||
"<style scoped>\n", | ||
" .dataframe tbody tr th:only-of-type {\n", | ||
" vertical-align: middle;\n", | ||
" }\n", | ||
"\n", | ||
" .dataframe tbody tr th {\n", | ||
" vertical-align: top;\n", | ||
" }\n", | ||
"\n", | ||
" .dataframe thead th {\n", | ||
" text-align: right;\n", | ||
" }\n", | ||
"</style>\n", | ||
"<table border=\"1\" class=\"dataframe\">\n", | ||
" <thead>\n", | ||
" <tr style=\"text-align: right;\">\n", | ||
" <th></th>\n", | ||
" <th>start_point</th>\n", | ||
" <th>end_point</th>\n", | ||
" </tr>\n", | ||
" </thead>\n", | ||
" <tbody>\n", | ||
" <tr>\n", | ||
" <th>0</th>\n", | ||
" <td>0</td>\n", | ||
" <td>1</td>\n", | ||
" </tr>\n", | ||
" <tr>\n", | ||
" <th>1</th>\n", | ||
" <td>3</td>\n", | ||
" <td>8</td>\n", | ||
" </tr>\n", | ||
" <tr>\n", | ||
" <th>2</th>\n", | ||
" <td>5</td>\n", | ||
" <td>7</td>\n", | ||
" </tr>\n", | ||
" <tr>\n", | ||
" <th>3</th>\n", | ||
" <td>10</td>\n", | ||
" <td>11</td>\n", | ||
" </tr>\n", | ||
" </tbody>\n", | ||
"</table>\n", | ||
"</div>" | ||
], | ||
"text/plain": [ | ||
" start_point end_point\n", | ||
"0 0 1\n", | ||
"1 3 8\n", | ||
"2 5 7\n", | ||
"3 10 11" | ||
] | ||
}, | ||
"execution_count": 41, | ||
"metadata": {}, | ||
"output_type": "execute_result" | ||
} | ||
], | ||
"source": [ | ||
"tmp = intervals.sort_values(\"start_point\").reset_index(drop=True)\n", | ||
"tmp" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"id": "8149bf5c-87ee-4115-9509-7c0f1914a621", | ||
"metadata": {}, | ||
"source": [ | ||
"Now, we compare start point $i$ with end point $i - 1$." | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 42, | ||
"id": "43bd1130-ba66-45d0-9c50-ca62270603a6", | ||
"metadata": {}, | ||
"outputs": [ | ||
{ | ||
"data": { | ||
"text/plain": [ | ||
"0 NaN\n", | ||
"1 1.0\n", | ||
"2 8.0\n", | ||
"3 7.0\n", | ||
"Name: end_point, dtype: float64" | ||
] | ||
}, | ||
"execution_count": 42, | ||
"metadata": {}, | ||
"output_type": "execute_result" | ||
} | ||
], | ||
"source": [ | ||
"tmp[\"end_point\"].shift()" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"id": "7581702c-abe3-4e48-987a-47aa9c2338d9", | ||
"metadata": {}, | ||
"source": [ | ||
"We can use this to tell where the overlapping intervals are. For example, in rows 1 the start point 3 is greater than 1 so the $0$-th and $1$-st intervals do not overlap. However, in row 2 the end point 8 is greater than the start point 5 so the $1$-st and $2$-nd intervals overlap.\n", | ||
"\n", | ||
"Row 3 interesting because the end point previous end point is greater than it. Since we sorted the start points, end an end point is less than its neighbour, we can guarantee it is contained within the interval. So, we include a `cummax`." | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 49, | ||
"id": "2a96a6d0-4caf-4079-8bc7-044bb152cce1", | ||
"metadata": {}, | ||
"outputs": [ | ||
{ | ||
"data": { | ||
"text/plain": [ | ||
"0 0\n", | ||
"1 1\n", | ||
"2 1\n", | ||
"3 2\n", | ||
"dtype: int64" | ||
] | ||
}, | ||
"execution_count": 49, | ||
"metadata": {}, | ||
"output_type": "execute_result" | ||
} | ||
], | ||
"source": [ | ||
"(tmp[\"start_point\"] > tmp[\"end_point\"].shift().cummax()).cumsum()" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"id": "b2321f65-26c0-43da-a8d0-f61add962d00", | ||
"metadata": {}, | ||
"source": [ | ||
"Now we can group together the intervals which overlap." | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 53, | ||
"id": "6472bf95-259d-4c40-985c-186be9ad6bc4", | ||
"metadata": {}, | ||
"outputs": [ | ||
{ | ||
"data": { | ||
"text/html": [ | ||
"<div>\n", | ||
"<style scoped>\n", | ||
" .dataframe tbody tr th:only-of-type {\n", | ||
" vertical-align: middle;\n", | ||
" }\n", | ||
"\n", | ||
" .dataframe tbody tr th {\n", | ||
" vertical-align: top;\n", | ||
" }\n", | ||
"\n", | ||
" .dataframe thead th {\n", | ||
" text-align: right;\n", | ||
" }\n", | ||
"</style>\n", | ||
"<table border=\"1\" class=\"dataframe\">\n", | ||
" <thead>\n", | ||
" <tr style=\"text-align: right;\">\n", | ||
" <th></th>\n", | ||
" <th>start_point</th>\n", | ||
" <th>end_point</th>\n", | ||
" </tr>\n", | ||
" </thead>\n", | ||
" <tbody>\n", | ||
" <tr>\n", | ||
" <th>0</th>\n", | ||
" <td>0</td>\n", | ||
" <td>1</td>\n", | ||
" </tr>\n", | ||
" <tr>\n", | ||
" <th>1</th>\n", | ||
" <td>3</td>\n", | ||
" <td>8</td>\n", | ||
" </tr>\n", | ||
" <tr>\n", | ||
" <th>2</th>\n", | ||
" <td>10</td>\n", | ||
" <td>11</td>\n", | ||
" </tr>\n", | ||
" </tbody>\n", | ||
"</table>\n", | ||
"</div>" | ||
], | ||
"text/plain": [ | ||
" start_point end_point\n", | ||
"0 0 1\n", | ||
"1 3 8\n", | ||
"2 10 11" | ||
] | ||
}, | ||
"execution_count": 53, | ||
"metadata": {}, | ||
"output_type": "execute_result" | ||
} | ||
], | ||
"source": [ | ||
"groups = (tmp[\"start_point\"] > tmp[\"end_point\"].shift().cummax()).cumsum()\n", | ||
"tmp.groupby(groups).agg({\"start_point\": \"min\", \"end_point\": \"max\"})" | ||
] | ||
} | ||
], | ||
"metadata": { | ||
"kernelspec": { | ||
"display_name": "Python 3 (ipykernel)", | ||
"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.10.12" | ||
} | ||
}, | ||
"nbformat": 4, | ||
"nbformat_minor": 5 | ||
} |
Oops, something went wrong.