Created
June 16, 2022 16:59
-
-
Save remram44/8799dec62b008ffd337a3fc6b06cd22b to your computer and use it in GitHub Desktop.
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
{ | |
"cells": [ | |
{ | |
"cell_type": "code", | |
"execution_count": 25, | |
"id": "23b443fa", | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"import numpy\n", | |
"import pandas" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 5, | |
"id": "13087362", | |
"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>TIMESTAMP</th>\n", | |
" <th>TEMP-F</th>\n", | |
" <th>RH-PC</th>\n", | |
" </tr>\n", | |
" </thead>\n", | |
" <tbody>\n", | |
" <tr>\n", | |
" <th>0</th>\n", | |
" <td>2017-11-21 17:30:00</td>\n", | |
" <td>72.8</td>\n", | |
" <td>25</td>\n", | |
" </tr>\n", | |
" <tr>\n", | |
" <th>1</th>\n", | |
" <td>2017-11-21 18:00:00</td>\n", | |
" <td>71.7</td>\n", | |
" <td>24</td>\n", | |
" </tr>\n", | |
" <tr>\n", | |
" <th>2</th>\n", | |
" <td>2017-11-21 18:30:00</td>\n", | |
" <td>71.4</td>\n", | |
" <td>24</td>\n", | |
" </tr>\n", | |
" <tr>\n", | |
" <th>3</th>\n", | |
" <td>2017-11-21 19:00:00</td>\n", | |
" <td>71.0</td>\n", | |
" <td>24</td>\n", | |
" </tr>\n", | |
" <tr>\n", | |
" <th>4</th>\n", | |
" <td>2017-11-21 19:30:00</td>\n", | |
" <td>70.8</td>\n", | |
" <td>24</td>\n", | |
" </tr>\n", | |
" </tbody>\n", | |
"</table>\n", | |
"</div>" | |
], | |
"text/plain": [ | |
" TIMESTAMP TEMP-F RH-PC\n", | |
"0 2017-11-21 17:30:00 72.8 25\n", | |
"1 2017-11-21 18:00:00 71.7 24\n", | |
"2 2017-11-21 18:30:00 71.4 24\n", | |
"3 2017-11-21 19:00:00 71.0 24\n", | |
"4 2017-11-21 19:30:00 70.8 24" | |
] | |
}, | |
"execution_count": 5, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"data = pandas.DataFrame({\n", | |
" 'TIMESTAMP': pandas.to_datetime([\n", | |
" '2017-11-21 17:30',\n", | |
" '2017-11-21 18:00',\n", | |
" '2017-11-21 18:30',\n", | |
" '2017-11-21 19:00',\n", | |
" '2017-11-21 19:30',\n", | |
" ]),\n", | |
" 'TEMP-F': [\n", | |
" 72.8,\n", | |
" 71.7,\n", | |
" 71.4,\n", | |
" 71.0,\n", | |
" 70.8,\n", | |
" ],\n", | |
" 'RH-PC': [\n", | |
" 25,\n", | |
" 24,\n", | |
" 24,\n", | |
" 24,\n", | |
" 24,\n", | |
" ],\n", | |
"})\n", | |
"data" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 23, | |
"id": "884da7fb", | |
"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>TIMESTAMP</th>\n", | |
" <th>TEMP-F</th>\n", | |
" <th>RH-PC</th>\n", | |
" </tr>\n", | |
" </thead>\n", | |
" <tbody>\n", | |
" <tr>\n", | |
" <th>0</th>\n", | |
" <td>2017-11-21 17:00:00</td>\n", | |
" <td>72.8</td>\n", | |
" <td>25</td>\n", | |
" </tr>\n", | |
" <tr>\n", | |
" <th>1</th>\n", | |
" <td>2017-11-21 18:00:00</td>\n", | |
" <td>71.7</td>\n", | |
" <td>24</td>\n", | |
" </tr>\n", | |
" <tr>\n", | |
" <th>2</th>\n", | |
" <td>2017-11-21 18:00:00</td>\n", | |
" <td>71.4</td>\n", | |
" <td>24</td>\n", | |
" </tr>\n", | |
" <tr>\n", | |
" <th>3</th>\n", | |
" <td>2017-11-21 19:00:00</td>\n", | |
" <td>71.0</td>\n", | |
" <td>24</td>\n", | |
" </tr>\n", | |
" <tr>\n", | |
" <th>4</th>\n", | |
" <td>2017-11-21 19:00:00</td>\n", | |
" <td>70.8</td>\n", | |
" <td>24</td>\n", | |
" </tr>\n", | |
" </tbody>\n", | |
"</table>\n", | |
"</div>" | |
], | |
"text/plain": [ | |
" TIMESTAMP TEMP-F RH-PC\n", | |
"0 2017-11-21 17:00:00 72.8 25\n", | |
"1 2017-11-21 18:00:00 71.7 24\n", | |
"2 2017-11-21 18:00:00 71.4 24\n", | |
"3 2017-11-21 19:00:00 71.0 24\n", | |
"4 2017-11-21 19:00:00 70.8 24" | |
] | |
}, | |
"execution_count": 23, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"# Map to hourly timestamps\n", | |
"def map_hourly(row):\n", | |
" t = row['TIMESTAMP']\n", | |
" row['TIMESTAMP'] = pandas.Timestamp(\n", | |
" tz=t.tz,\n", | |
" year=t.year,\n", | |
" month=t.month,\n", | |
" day=t.day,\n", | |
" hour=t.hour,\n", | |
" )\n", | |
" return row\n", | |
"mapped_data = data.copy().apply(map_hourly, axis=1)\n", | |
"mapped_data" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 29, | |
"id": "009ca1e0", | |
"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>TIMESTAMP</th>\n", | |
" <th>TEMP-F</th>\n", | |
" <th>RH-PC</th>\n", | |
" </tr>\n", | |
" </thead>\n", | |
" <tbody>\n", | |
" <tr>\n", | |
" <th>0</th>\n", | |
" <td>2017-11-21 17:00:00</td>\n", | |
" <td>72.80</td>\n", | |
" <td>25.0</td>\n", | |
" </tr>\n", | |
" <tr>\n", | |
" <th>1</th>\n", | |
" <td>2017-11-21 18:00:00</td>\n", | |
" <td>71.55</td>\n", | |
" <td>24.0</td>\n", | |
" </tr>\n", | |
" <tr>\n", | |
" <th>2</th>\n", | |
" <td>2017-11-21 19:00:00</td>\n", | |
" <td>70.90</td>\n", | |
" <td>24.0</td>\n", | |
" </tr>\n", | |
" </tbody>\n", | |
"</table>\n", | |
"</div>" | |
], | |
"text/plain": [ | |
" TIMESTAMP TEMP-F RH-PC\n", | |
"0 2017-11-21 17:00:00 72.80 25.0\n", | |
"1 2017-11-21 18:00:00 71.55 24.0\n", | |
"2 2017-11-21 19:00:00 70.90 24.0" | |
] | |
}, | |
"execution_count": 29, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"# Aggregate\n", | |
"aggregated_data = mapped_data.groupby(by='TIMESTAMP').agg(numpy.mean).reset_index()\n", | |
"aggregated_data" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"id": "4d77df09", | |
"metadata": {}, | |
"outputs": [], | |
"source": [] | |
} | |
], | |
"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.8.2" | |
} | |
}, | |
"nbformat": 4, | |
"nbformat_minor": 5 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment