Skip to content

Commit

Permalink
Merge pull request #103 from callysto/january-notebook-fix
Browse files Browse the repository at this point in the history
Fixed Errors in January-Temp Notebook
  • Loading branch information
byrcyb authored Jan 2, 2024
2 parents b6fb8ba + 2d6af3b commit 270ac23
Showing 1 changed file with 30 additions and 12 deletions.
42 changes: 30 additions & 12 deletions january-temperatures/january-temperatures.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -33,16 +33,23 @@
"\n",
"station = 27793\n",
"df = pd.DataFrame()\n",
"\n",
"for year in range(1999, 2024):\n",
" url = 'https://climate.weather.gc.ca/climate_data/bulk_data_e.html?format=csv&stationID='+str(station)+'&Year='+str(year)+'&Month=1&Day=1&time=&timeframe=2&submit=Download+Data'\n",
" df = pd.concat([df, pd.read_csv(url)])\n",
"temperatures = df[df['Month']==1].groupby('Year').mean()\n",
" url = f'https://climate.weather.gc.ca/climate_data/bulk_data_e.html?format=csv&stationID={station}&Year={year}&Month=1&Day=1&time=&timeframe=2&submit=Download+Data'\n",
" yearly_data = pd.read_csv(url)\n",
" yearly_data = yearly_data.dropna(subset=['Max Temp (°C)', 'Mean Temp (°C)', 'Min Temp (°C)'])\n",
" df = pd.concat([df, yearly_data])\n",
"\n",
"fig = px.line(temperatures, y=['Max Temp (°C)','Mean Temp (°C)','Min Temp (°C)'], color_discrete_sequence=['red', 'green', 'blue'], title='January Temperatures in Edmonton')\n",
"temperature_columns = ['Max Temp (°C)', 'Mean Temp (°C)', 'Min Temp (°C)']\n",
"january_temperatures = df[df['Month'] == 1]\n",
"temperatures = january_temperatures.groupby('Year')[temperature_columns].mean().reset_index()\n",
"fig = px.line(temperatures, x='Year', y=temperature_columns, title='January Temperatures in Edmonton',\n",
" color_discrete_sequence=['red', 'green', 'blue'])\n",
"fig.update_layout(yaxis_title='Temperature (°C)')\n",
"fig.add_hline(temperatures['Max Temp (°C)'].max(), line_dash='dash', line_color='red')\n",
"fig.add_hline(temperatures['Mean Temp (°C)'].max(), line_dash='dash', line_color='green')\n",
"fig.add_hline(temperatures['Min Temp (°C)'].max(), line_dash='dash', line_color='blue')\n",
"\n",
"for col in temperature_columns:\n",
" fig.add_hline(temperatures[col].max(), line_dash='dash', line_color=fig.data[0].line.color)\n",
"\n",
"fig.show()"
]
},
Expand Down Expand Up @@ -100,10 +107,16 @@
"months = ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December']\n",
"print('Getting data for', station_name, 'from', start_year, 'to', end_year, 'for', months[month-1])\n",
"df = pd.DataFrame()\n",
"\n",
"for year in range(start_year, end_year+1):\n",
" url = 'https://climate.weather.gc.ca/climate_data/bulk_data_e.html?format=csv&stationID='+str(station)+'&Year='+str(year)+'&Month='+str(month)+'&Day=1&time=&timeframe=2&submit=Download+Data'\n",
" df = pd.concat([df, pd.read_csv(url)])\n",
"temperatures = df[df['Month']==1].groupby('Year').mean()\n",
" url = f'https://climate.weather.gc.ca/climate_data/bulk_data_e.html?format=csv&stationID={station}&Year={year}&Month={month}&Day=1&time=&timeframe=2&submit=Download+Data'\n",
" yearly_data = pd.read_csv(url)\n",
" yearly_data = yearly_data.dropna(subset=['Max Temp (°C)', 'Mean Temp (°C)', 'Min Temp (°C)'])\n",
" df = pd.concat([df, yearly_data])\n",
"\n",
"temperature_columns = ['Max Temp (°C)', 'Mean Temp (°C)', 'Min Temp (°C)']\n",
"temperatures = df.groupby('Year')[temperature_columns].mean().reset_index()\n",
"\n",
"fig = px.line(temperatures, y=['Max Temp (°C)','Mean Temp (°C)','Min Temp (°C)'], color_discrete_sequence=['red', 'green', 'blue'], title='Average Temperatures for '+station_name+' in '+months[month-1])\n",
"fig.update_layout(yaxis_title='Temperature (°C)')\n",
"fig.add_hline(temperatures['Max Temp (°C)'].max(), line_dash='dash', line_color='red')\n",
Expand Down Expand Up @@ -164,7 +177,12 @@
"outputs": [],
"source": [
"month = 1\n",
"temperatures = df[df['Month']==month].groupby(['Station Name', 'Year']).mean()['Mean Temp (°C)'].unstack().T\n",
"\n",
"temperatures = df[df['Month'] == month].groupby(['Station Name', 'Year'])[['Mean Temp (°C)']].mean().reset_index()\n",
"temperatures['Mean Temp (°C)'] = pd.to_numeric(temperatures['Mean Temp (°C)'], errors='coerce')\n",
"temperatures = temperatures.dropna(subset=['Mean Temp (°C)'])\n",
"\n",
"temperatures = temperatures.pivot(index='Year', columns='Station Name', values='Mean Temp (°C)')\n",
"px.line(temperatures, title='Average '+months[month-1]+' Temperatures at '+station_name1+' and '+station_name2).update_layout(yaxis_title='Mean Temperature (°C)').show()\n",
"temperatures"
]
Expand Down Expand Up @@ -204,7 +222,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.10.1"
"version": "3.10.8"
},
"vscode": {
"interpreter": {
Expand Down

0 comments on commit 270ac23

Please sign in to comment.