by RJ Weise, October 30th 2015
This is the project notebook for Week 2 of The Open University's Learn to code for Data Analysis course.
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 Calgary. 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 Calgary 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.
From http://www.wunderground.com/history I got weather data for Calgary for 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. Scroll to the end of the data and then right click on the blue link labelled 'Comma Delimited File':
then, in the File dialogue that appears save the file with its default name of 'CustomHistory' to the folder you created for this course and where this notebook is located. Once the file has been downloaded rename it from 'CustomHistory.html' to 'London_2014.csv'.
Now load the CSV file into a dataframe making sure that any extra spaces are skipped:
from pandas import *
yyc = read_csv('Calgary.csv', skipinitialspace=True)
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'
.
yyc = yyc.rename(columns={'WindDirDegrees<br>' : 'WindDirDegrees'})
remove the < br />
html line breaks from the values in the 'WindDirDegrees'
column.
#yyc['WindDirDegrees'] = yyc['WindDirDegrees'].str.rstrip('<br>')
and change the values in the 'WindDirDegrees'
column to float64
:
yyc['WindDirDegrees'] = yyc['WindDirDegrees'].astype('float64')
We definitely need to change the values in the 'GMT'
column into values of the datetime64
date type.
yyc['MST'] = to_datetime(yyc['MST'])
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:
yyc.index = yyc['MST']
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:
summer = yyc.ix[datetime(2014,6,1) : datetime(2014,8,31)]
I now look for the days with warm temperatures.
summer[summer['Mean TemperatureC'] >= 25]
Summer 2014 was rather cool in London: there are no days with temperatures of 25 Celsius or higher. Best to see a graph of the temperature and look for the warmest period.
So next we tell Jupyter to display any graph created inside this notebook:
%matplotlib inline
Now let's plot the 'Mean TemperatureC'
for the summer:
summer['Mean TemperatureC'].plot(grid=True, figsize=(10,5))
Well looking at the graph the last week of July looks good for mean temperatures over 20 degrees C so let's also put precipitation on the graph too:
summer[['Mean TemperatureC', 'Precipitationmm']].plot(grid=True, figsize=(10,5))
The whole month of July is looking good, with a few peaks of rain. Lets have a closer look by just plotting mean temperature and precipitation for July.
july = summer.ix[datetime(2014,7,1) : datetime(2014,7,31)]
july[['Mean TemperatureC', 'Precipitationmm']].plot(grid=True, figsize=(10,5))
The first two weeks of July look good here, stable temperature between 17 and 21, and hardly any rain.
The graphs have shown the first two weeks of July will be dry and fairly warm in Calgary.