{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Project 2: Holiday weather\n",
"\n",
"by Rob Griffiths, 11 September 2015, updated 11 April 2017, 18 October and 20 December 2017\n",
"\n",
"This is the project notebook for the second part of The Open University's _Learn to code for Data Analysis_ course.\n",
"\n",
"There is nothing I like better than taking a holiday. In the winter I like to have a two week break in a country where I can be guaranteed sunny dry days. In the summer I like to have two weeks off relaxing in my garden in London. However I'm often disappointed because I pick a fortnight when the weather is dull and it rains. So in this project I am going to use the historic weather data from the Weather Underground for London to try to predict two good weather weeks to take off as holiday next summer. Of course the weather in the summer of 2016 may be very different to 2014 but it should give me some indication of when would be a good time to take a summer break.\n",
"\n",
"## Getting the data\n",
"\n",
"Weather Underground keeps historical weather data collected in many airports around the world. Right-click on the following URL and choose 'Open Link in New Window' (or similar, depending on your browser):\n",
"\n",
"http://www.wunderground.com/history\n",
"\n",
"When the new page opens start typing 'LHR' in the 'Location' input box and when the pop up menu comes up with the option 'LHR, United Kingdom' select it and then click on 'Submit'. \n",
"\n",
"When the next page opens with London Heathrow data, click on the 'Custom' tab and select the time period From: 1 January 2014 to: 31 December 2014 and then click on 'Get History'. The data for that year should then be displayed further down the page. \n",
"\n",
"You can copy each month's data directly from the browser to a text editor like Notepad or TextEdit, to obtain a single file with as many months as you wish.\n",
"\n",
"Weather Underground has changed in the past the way it provides data and may do so again in the future. \n",
"I have therefore collated the whole 2014 data in the provided 'London_2014.csv' file. \n",
"\n",
"Now load the CSV file into a dataframe making sure that any extra spaces are skipped:"
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"import warnings\n",
"warnings.simplefilter('ignore', FutureWarning)\n",
"\n",
"from pandas import *\n",
"london = read_csv('London_2014.csv', skipinitialspace=True)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Cleaning the data\n",
"First we need to clean up the data. I'm not going to make use of `'WindDirDegrees'` in my analysis, but you might in yours so we'll rename `'WindDirDegrees< br />'` to `'WindDirDegrees'`. "
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"london = london.rename(columns={'WindDirDegrees
' : 'WindDirDegrees'})"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"remove the `< br />` html line breaks from the values in the `'WindDirDegrees'` column. "
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"london['WindDirDegrees'] = london['WindDirDegrees'].str.rstrip('
')"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"and change the values in the `'WindDirDegrees'` column to `float64`:"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"london['WindDirDegrees'] = london['WindDirDegrees'].astype('float64') "
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"We definitely need to change the values in the `'GMT'` column into values of the `datetime64` date type."
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"london['GMT'] = to_datetime(london['GMT'])"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"We also need to change the index from the default to the `datetime64` values in the `'GMT'` column so that it is easier to pull out rows between particular dates and display more meaningful graphs: "
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"london.index = london['GMT']"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Finding a summer break\n",
"\n",
"According to meteorologists, summer extends for the whole months of June, July, and August in the northern hemisphere and the whole months of December, January, and February in the southern hemisphere. So as I'm in the northern hemisphere I'm going to create a dataframe that holds just those months using the `datetime` index, like this:"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"summer = london.loc[datetime(2014,6,1) : datetime(2014,8,31)]"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"I now look for the days with warm temperatures."
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"
\n", " | GMT | \n", "Max TemperatureC | \n", "Mean TemperatureC | \n", "Min TemperatureC | \n", "Dew PointC | \n", "MeanDew PointC | \n", "Min DewpointC | \n", "Max Humidity | \n", "Mean Humidity | \n", "Min Humidity | \n", "... | \n", "Max VisibilityKm | \n", "Mean VisibilityKm | \n", "Min VisibilitykM | \n", "Max Wind SpeedKm/h | \n", "Mean Wind SpeedKm/h | \n", "Max Gust SpeedKm/h | \n", "Precipitationmm | \n", "CloudCover | \n", "Events | \n", "WindDirDegrees | \n", "
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
GMT | \n", "\n", " | \n", " | \n", " | \n", " | \n", " | \n", " | \n", " | \n", " | \n", " | \n", " | \n", " | \n", " | \n", " | \n", " | \n", " | \n", " | \n", " | \n", " | \n", " | \n", " | \n", " |
0 rows × 23 columns
\n", "