Skip to content

Instantly share code, notes, and snippets.

@dannymorris
Created December 2, 2021 04:46
Show Gist options
  • Save dannymorris/795af4072b238c6d66b5278428086b7f to your computer and use it in GitHub Desktop.
Save dannymorris/795af4072b238c6d66b5278428086b7f to your computer and use it in GitHub Desktop.
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"id": "f87b5376-f3b3-4c93-a3f7-f3f3dad5c396",
"metadata": {},
"outputs": [],
"source": [
"%load_ext autoreload\n",
"%autoreload 2"
]
},
{
"cell_type": "code",
"execution_count": 35,
"id": "1e498bf2-a444-451e-85e0-bd6f9e6cafce",
"metadata": {},
"outputs": [],
"source": [
"from datetime import datetime, timedelta\n",
"import pandas as pd\n",
"import numpy as np\n",
"from meteostat import Stations, Daily, Hourly, units\n",
"\n",
"from hourly import *\n",
"from daily import *\n",
"from stations import *"
]
},
{
"cell_type": "code",
"execution_count": 36,
"id": "314eaf82-ee2c-433e-bbf4-5dc0f933d0e7",
"metadata": {},
"outputs": [],
"source": [
"stations = get_stations()"
]
},
{
"cell_type": "code",
"execution_count": 37,
"id": "f7c6c465-80c3-4d74-a3ca-7fa6b6678ab8",
"metadata": {},
"outputs": [],
"source": [
"station_id = ['KIAG0']\n",
"\n",
"hourly_weather = get_hourly_weather(station = station_id, \n",
" days_history = 60)"
]
},
{
"cell_type": "code",
"execution_count": 38,
"id": "3b3422fa-fced-43f9-b573-4133828023cf",
"metadata": {},
"outputs": [],
"source": [
"daily_prec = (hourly_weather\n",
" .groupby('time_date')\n",
" .agg({'prcp': 'sum'})\n",
" .pipe(lambda x: x.assign(total_prcp_7_days = x['prcp'].rolling(7).sum()))\n",
" .pipe(lambda x: x.assign(total_prcp_30_days = x['prcp'].rolling(30).sum()))\n",
" .reset_index()\n",
" .sort_values(['time_date'], ascending = True)\n",
" .tail(30)\n",
" .rename(columns = {'prcp': \"Total_Daily\",\n",
" 'total_prcp_7_days': \"7_Day_Total\",\n",
" 'total_prcp_30_days': '30_Day_Total'})\n",
" .pipe(lambda x: pd.melt(x, id_vars = ['time_date'], value_vars = ['Total_Daily', '7_Day_Total', '30_Day_Total']))\n",
")"
]
},
{
"cell_type": "code",
"execution_count": 44,
"id": "4749f56e-c70d-4105-a655-ad15bbd3f751",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Help on method vega_lite_chart in module streamlit.delta_generator:\n",
"\n",
"vega_lite_chart(data=None, spec=None, use_container_width=False, **kwargs) method of streamlit.delta_generator.DeltaGenerator instance\n",
" Display a chart using the Vega-Lite library.\n",
" \n",
" Parameters\n",
" ----------\n",
" data : pandas.DataFrame, pandas.Styler, numpy.ndarray, Iterable, dict,\n",
" or None\n",
" Either the data to be plotted or a Vega-Lite spec containing the\n",
" data (which more closely follows the Vega-Lite API).\n",
" \n",
" spec : dict or None\n",
" The Vega-Lite spec for the chart. If the spec was already passed in\n",
" the previous argument, this must be set to None. See\n",
" https://vega.github.io/vega-lite/docs/ for more info.\n",
" \n",
" use_container_width : bool\n",
" If True, set the chart width to the column width. This takes\n",
" precedence over Vega-Lite's native `width` value.\n",
" \n",
" **kwargs : any\n",
" Same as spec, but as keywords.\n",
" \n",
" Example\n",
" -------\n",
" \n",
" >>> import pandas as pd\n",
" >>> import numpy as np\n",
" >>>\n",
" >>> df = pd.DataFrame(\n",
" ... np.random.randn(200, 3),\n",
" ... columns=['a', 'b', 'c'])\n",
" >>>\n",
" >>> st.vega_lite_chart(df, {\n",
" ... 'mark': {'type': 'circle', 'tooltip': True},\n",
" ... 'encoding': {\n",
" ... 'x': {'field': 'a', 'type': 'quantitative'},\n",
" ... 'y': {'field': 'b', 'type': 'quantitative'},\n",
" ... 'size': {'field': 'c', 'type': 'quantitative'},\n",
" ... 'color': {'field': 'c', 'type': 'quantitative'},\n",
" ... },\n",
" ... })\n",
" \n",
" .. output::\n",
" https://static.streamlit.io/0.25.0-2JkNY/index.html?id=8jmmXR8iKoZGV4kXaKGYV5\n",
" height: 200px\n",
" \n",
" Examples of Vega-Lite usage without Streamlit can be found at\n",
" https://vega.github.io/vega-lite/examples/. Most of those can be easily\n",
" translated to the syntax shown above.\n",
"\n"
]
}
],
"source": [
"import streamlit as st\n",
"help(st.vega_lite_chart)"
]
},
{
"cell_type": "code",
"execution_count": 23,
"id": "8494e408-5df7-4654-8793-34405a9496fc",
"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>time_date</th>\n",
" <th>variable</th>\n",
" <th>value</th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th>0</th>\n",
" <td>2021-07-19</td>\n",
" <td>prcp</td>\n",
" <td>0.024</td>\n",
" </tr>\n",
" <tr>\n",
" <th>1</th>\n",
" <td>2021-07-18</td>\n",
" <td>prcp</td>\n",
" <td>0.008</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2</th>\n",
" <td>2021-07-17</td>\n",
" <td>prcp</td>\n",
" <td>3.290</td>\n",
" </tr>\n",
" <tr>\n",
" <th>3</th>\n",
" <td>2021-07-16</td>\n",
" <td>prcp</td>\n",
" <td>0.216</td>\n",
" </tr>\n",
" <tr>\n",
" <th>4</th>\n",
" <td>2021-07-15</td>\n",
" <td>prcp</td>\n",
" <td>0.000</td>\n",
" </tr>\n",
" <tr>\n",
" <th>...</th>\n",
" <td>...</td>\n",
" <td>...</td>\n",
" <td>...</td>\n",
" </tr>\n",
" <tr>\n",
" <th>85</th>\n",
" <td>2021-06-24</td>\n",
" <td>total_prcp_30_days</td>\n",
" <td>2.990</td>\n",
" </tr>\n",
" <tr>\n",
" <th>86</th>\n",
" <td>2021-06-23</td>\n",
" <td>total_prcp_30_days</td>\n",
" <td>2.990</td>\n",
" </tr>\n",
" <tr>\n",
" <th>87</th>\n",
" <td>2021-06-22</td>\n",
" <td>total_prcp_30_days</td>\n",
" <td>2.986</td>\n",
" </tr>\n",
" <tr>\n",
" <th>88</th>\n",
" <td>2021-06-21</td>\n",
" <td>total_prcp_30_days</td>\n",
" <td>2.986</td>\n",
" </tr>\n",
" <tr>\n",
" <th>89</th>\n",
" <td>2021-06-20</td>\n",
" <td>total_prcp_30_days</td>\n",
" <td>2.820</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"<p>90 rows × 3 columns</p>\n",
"</div>"
],
"text/plain": [
" time_date variable value\n",
"0 2021-07-19 prcp 0.024\n",
"1 2021-07-18 prcp 0.008\n",
"2 2021-07-17 prcp 3.290\n",
"3 2021-07-16 prcp 0.216\n",
"4 2021-07-15 prcp 0.000\n",
".. ... ... ...\n",
"85 2021-06-24 total_prcp_30_days 2.990\n",
"86 2021-06-23 total_prcp_30_days 2.990\n",
"87 2021-06-22 total_prcp_30_days 2.986\n",
"88 2021-06-21 total_prcp_30_days 2.986\n",
"89 2021-06-20 total_prcp_30_days 2.820\n",
"\n",
"[90 rows x 3 columns]"
]
},
"execution_count": 23,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"daily_prec"
]
}
],
"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.9.2"
}
},
"nbformat": 4,
"nbformat_minor": 5
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment