Skip to content

Instantly share code, notes, and snippets.

@vinaykudari
Last active July 27, 2018 20:36
Show Gist options
  • Save vinaykudari/2631e7f8e4f5d6492c60e67bafb2789a to your computer and use it in GitHub Desktop.
Save vinaykudari/2631e7f8e4f5d6492c60e67bafb2789a to your computer and use it in GitHub Desktop.
Rsampling
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [],
"source": [
"import pandas as pd\n",
"import numpy as np"
]
},
{
"cell_type": "code",
"execution_count": 11,
"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>units</th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th>2014-05-01 00:00:00</th>\n",
" <td>19</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2014-05-01 00:30:00</th>\n",
" <td>11</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2014-05-01 01:00:00</th>\n",
" <td>12</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2014-05-01 01:30:00</th>\n",
" <td>4</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2014-05-01 02:00:00</th>\n",
" <td>10</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"</div>"
],
"text/plain": [
" units\n",
"2014-05-01 00:00:00 19\n",
"2014-05-01 00:30:00 11\n",
"2014-05-01 01:00:00 12\n",
"2014-05-01 01:30:00 4\n",
"2014-05-01 02:00:00 10"
]
},
"execution_count": 11,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"date_index = pd.date_range('2014-05-01', '2014-05-31', freq='30min')\n",
"units = np.random.randint(0, 20, len(date_index))\n",
"\n",
"df = pd.DataFrame(index=date_index)\n",
"df['units'] = units\n",
"df.head()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Downsampling"
]
},
{
"cell_type": "code",
"execution_count": 32,
"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>units</th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th>2014-05-04</th>\n",
" <td>10.083333</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2014-05-18</th>\n",
" <td>9.563988</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2014-06-01</th>\n",
" <td>9.662045</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"</div>"
],
"text/plain": [
" units\n",
"2014-05-04 10.083333\n",
"2014-05-18 9.563988\n",
"2014-06-01 9.662045"
]
},
"execution_count": 32,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"df.resample('2W').mean() #resampled every two weeks"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Upsampling"
]
},
{
"cell_type": "code",
"execution_count": 38,
"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>units</th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th>2014-05-01 00:00:00</th>\n",
" <td>19.000000</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2014-05-01 00:10:00</th>\n",
" <td>14.904594</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2014-05-01 00:20:00</th>\n",
" <td>12.237927</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2014-05-01 00:30:00</th>\n",
" <td>11.000000</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2014-05-01 00:40:00</th>\n",
" <td>11.190812</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"</div>"
],
"text/plain": [
" units\n",
"2014-05-01 00:00:00 19.000000\n",
"2014-05-01 00:10:00 14.904594\n",
"2014-05-01 00:20:00 12.237927\n",
"2014-05-01 00:30:00 11.000000\n",
"2014-05-01 00:40:00 11.190812"
]
},
"execution_count": 38,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"df.resample('10T').interpolate('quadratic').head()"
]
}
],
"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.6.5"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment