Last active
November 10, 2022 09:15
-
-
Save GabrielSGoncalves/2543970817455f3742e8c538d7a88f58 to your computer and use it in GitHub Desktop.
This file contains hidden or 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": "markdown", | |
"metadata": {}, | |
"source": [ | |
"## 1. Accessing pyown API\n", | |
"On the first steps of our analysis we are going to get weather information for the define city using pyown API. <br>\n", | |
"The information is going to be organized on a dictionary, and later loaded as a pandas DataFrame for performing the Data wrangling." | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 1, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"# import libraries\n", | |
"import pyowm\n", | |
"import pandas as pd\n", | |
"import seaborn as sns\n", | |
"import matplotlib.pyplot as plt\n", | |
"import pdfkit " | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"### 1.1 Setting PyOWM API key and defining variable\n", | |
"This first part of the workflow we define the API key to access OpenWeather service and also the cell to contain the parameter `city` that can be defined using Papermill." | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 2, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"# Set API key\n", | |
"owm = pyowm.OWM('xxxxxxxxxxxPyOWM-API-keyxxxxxxxx')" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 3, | |
"metadata": { | |
"tags": [ | |
"parameters" | |
] | |
}, | |
"outputs": [], | |
"source": [ | |
"# Define the default parameters\n", | |
"city = 'Sao Paulo,BR'" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"### 1.2 Fetching weather information for the defined city\n", | |
"On this part we are going to use PyOWM to fetch data about the city and organized it first on a dictionary, them on a Pandas DataFrame." | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 4, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"# Instantiate forecast object and fetch weather information about city\n", | |
"fc = owm.three_hours_forecast(city)\n", | |
"forecast = fc.get_forecast()" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 5, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"# Create a dictionary to organize the forecast data for the city of interest\n", | |
"dict_forecast = {\n", | |
" 'datetime':[],\n", | |
" 'clouds':[],\n", | |
" 'humidity':[],\n", | |
" 'temp':[],\n", | |
" 'temp_max':[],\n", | |
" 'temp_min':[],\n", | |
" 'detailed_status':[],\n", | |
" 'icon_url':[],\n", | |
" 'rain_vol':[]\n", | |
"}" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 6, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"#Itereate over forecast object acessing the weather features\n", | |
"for weather in forecast:\n", | |
" dict_forecast['datetime'].append(str(weather.get_reference_time(timeformat='iso')))\n", | |
" dict_forecast['clouds'].append(weather.get_clouds())\n", | |
" dict_forecast['humidity'].append(weather.get_humidity())\n", | |
" dict_forecast['temp'].append(weather.get_temperature(unit='celsius').get('temp'))\n", | |
" dict_forecast['temp_max'].append(weather.get_temperature(unit='celsius').get('temp_max'))\n", | |
" dict_forecast['temp_min'].append(weather.get_temperature(unit='celsius').get('temp_min'))\n", | |
" \n", | |
" dict_forecast['detailed_status'].append(weather.get_detailed_status())\n", | |
" dict_forecast['icon_url'].append(weather.get_weather_icon_url())\n", | |
" if '3h' in weather.get_rain().keys():\n", | |
" dict_forecast['rain_vol'].append(weather.get_rain().get('3h'))\n", | |
" else:\n", | |
" dict_forecast['rain_vol'].append(0)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 7, | |
"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>datetime</th>\n", | |
" <th>clouds</th>\n", | |
" <th>humidity</th>\n", | |
" <th>temp</th>\n", | |
" <th>temp_max</th>\n", | |
" <th>temp_min</th>\n", | |
" <th>detailed_status</th>\n", | |
" <th>icon_url</th>\n", | |
" <th>rain_vol</th>\n", | |
" </tr>\n", | |
" </thead>\n", | |
" <tbody>\n", | |
" <tr>\n", | |
" <th>0</th>\n", | |
" <td>2019-12-09 21:00:00+00</td>\n", | |
" <td>100</td>\n", | |
" <td>82</td>\n", | |
" <td>21.21</td>\n", | |
" <td>21.21</td>\n", | |
" <td>21.03</td>\n", | |
" <td>light rain</td>\n", | |
" <td>http://openweathermap.org/img/w/10d.png</td>\n", | |
" <td>2.25</td>\n", | |
" </tr>\n", | |
" <tr>\n", | |
" <th>1</th>\n", | |
" <td>2019-12-10 00:00:00+00</td>\n", | |
" <td>100</td>\n", | |
" <td>83</td>\n", | |
" <td>20.24</td>\n", | |
" <td>20.24</td>\n", | |
" <td>20.11</td>\n", | |
" <td>light rain</td>\n", | |
" <td>http://openweathermap.org/img/w/10n.png</td>\n", | |
" <td>0.06</td>\n", | |
" </tr>\n", | |
" <tr>\n", | |
" <th>2</th>\n", | |
" <td>2019-12-10 03:00:00+00</td>\n", | |
" <td>100</td>\n", | |
" <td>86</td>\n", | |
" <td>20.07</td>\n", | |
" <td>20.07</td>\n", | |
" <td>19.98</td>\n", | |
" <td>overcast clouds</td>\n", | |
" <td>http://openweathermap.org/img/w/04n.png</td>\n", | |
" <td>0.00</td>\n", | |
" </tr>\n", | |
" <tr>\n", | |
" <th>3</th>\n", | |
" <td>2019-12-10 06:00:00+00</td>\n", | |
" <td>100</td>\n", | |
" <td>86</td>\n", | |
" <td>19.91</td>\n", | |
" <td>19.91</td>\n", | |
" <td>19.87</td>\n", | |
" <td>overcast clouds</td>\n", | |
" <td>http://openweathermap.org/img/w/04n.png</td>\n", | |
" <td>0.00</td>\n", | |
" </tr>\n", | |
" <tr>\n", | |
" <th>4</th>\n", | |
" <td>2019-12-10 09:00:00+00</td>\n", | |
" <td>100</td>\n", | |
" <td>85</td>\n", | |
" <td>20.12</td>\n", | |
" <td>20.12</td>\n", | |
" <td>20.12</td>\n", | |
" <td>overcast clouds</td>\n", | |
" <td>http://openweathermap.org/img/w/04d.png</td>\n", | |
" <td>0.00</td>\n", | |
" </tr>\n", | |
" </tbody>\n", | |
"</table>\n", | |
"</div>" | |
], | |
"text/plain": [ | |
" datetime clouds humidity temp temp_max temp_min \\\n", | |
"0 2019-12-09 21:00:00+00 100 82 21.21 21.21 21.03 \n", | |
"1 2019-12-10 00:00:00+00 100 83 20.24 20.24 20.11 \n", | |
"2 2019-12-10 03:00:00+00 100 86 20.07 20.07 19.98 \n", | |
"3 2019-12-10 06:00:00+00 100 86 19.91 19.91 19.87 \n", | |
"4 2019-12-10 09:00:00+00 100 85 20.12 20.12 20.12 \n", | |
"\n", | |
" detailed_status icon_url rain_vol \n", | |
"0 light rain http://openweathermap.org/img/w/10d.png 2.25 \n", | |
"1 light rain http://openweathermap.org/img/w/10n.png 0.06 \n", | |
"2 overcast clouds http://openweathermap.org/img/w/04n.png 0.00 \n", | |
"3 overcast clouds http://openweathermap.org/img/w/04n.png 0.00 \n", | |
"4 overcast clouds http://openweathermap.org/img/w/04d.png 0.00 " | |
] | |
}, | |
"execution_count": 7, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"# Create Dataframe from dictionary\n", | |
"df = pd.DataFrame.from_dict(dict_forecast)\n", | |
"df.head()" | |
] | |
} | |
], | |
"metadata": { | |
"kernelspec": { | |
"display_name": "papermill", | |
"language": "python", | |
"name": "papermill" | |
}, | |
"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.7.5" | |
} | |
}, | |
"nbformat": 4, | |
"nbformat_minor": 4 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thanks for this.
I am getting
'OWM' object has no attribute 'three_hours_forecast'
Any ideas?