This is the project notebook for Week 2 of The Open University's Learn to code for Data Analysis course.
In this project I am going to use the historic weather data from the Weather Underground website for the historic city of Chester. In a rather rare burst of optimism, I decided to attempt to predict the best two good weeks to take off next summer. Of course the weather next year may be very different to 2015 but it should give me some indication of when would be a good time to take a summer break.
The 'Chester_2015.csv' source data can be obtained as follows. Right-click on the following URL and choose 'Open Link in New Window' (or similar, depending on your browser):
http://www.wunderground.com/history
When the new page opens, start typing 'Chester' in the 'Location' input box and when the pop up menu comes up with the option 'Chester, United Kingdom' select it and then click on 'Submit'.
When the next page opens click on the 'Custom' tab and selet the time period From: 1 January 2015 to: 31 December 2015 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 'Chester_2015.csv'.
from IPython.display import display, HTML
HTML('''Note: <p style="display:inline;color:darkred;">Option to toggle code visibility on/off is at <a href="#Bottom">bottom</a> of page.</p>''')
Next, we must load the CSV file into a dataframe, making sure that any extra spaces are skipped.
import warnings
warnings.simplefilter('ignore', FutureWarning)
from pandas import *
chester = read_csv('Chester_2015.csv', skipinitialspace=True)
First we need to clean up the data.
This involves:
'WindDirDegrees< br />'
to 'WindDirDegrees'
, if required; < br />
html line breaks from the values in the 'WindDirDegrees'
column;'WindDirDegrees'
column to float64
.chester = chester.rename(columns={'WindDirDegrees<br />' : 'WindDirDegrees'})
chester['WindDirDegrees'] = chester['WindDirDegrees'].str.rstrip('<br />')
chester['WindDirDegrees'] = chester['WindDirDegrees'].astype('float64')
chester['WindDirDegrees'].dtypes
We definitely need to change the values in the 'GMT'
column into values of the datetime64
date type.
chester['GMT'] = to_datetime(chester['GMT'])
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:
chester.index = chester['GMT']
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 = chester.ix[datetime(2015,6,1) : datetime(2015,8,31)]
I'll now look for the days with warm temperatures.
summer[summer['Mean TemperatureC'] >= 25]
Summer 2015 was rather cool in Chester: there are no days with temperatures of 25 °C 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:
ax=summer['Mean TemperatureC'].plot(grid=True, figsize=(10,6))
ax.set_ylabel('Mean Temperature ($^\circ$C)')
Looking at the graph above, it is apparent that the two weeks at the end of June have mean temperatures over 20 °C.
Let's also put precipitation on the graph:
ax=summer[['Mean TemperatureC', 'Precipitationmm']].plot(grid=True, figsize=(10,5))
myLabels = ['Mean Temperature ($^\circ$C)', 'Precipitation (mm)']
ax.legend(labels=myLabels)
The end of June / beginning of July still looks pretty good, with very little rain. Let's have a closer look by just plotting mean temperature and precipitation for the four weeks that span the end of June / beginning of July.
junejuly = summer.ix[datetime(2015,6,17) : datetime(2015,7,14)]
ax=junejuly[['Mean TemperatureC', 'Precipitationmm']].plot(grid=True, figsize=(10,5))
myLabels = ['Mean Temperature ($^\circ$C)', 'Precipitation (mm)']
ax.legend(labels=myLabels)
print("Average (mean) temperature over this period is about {0:.1f}" .format(junejuly['Mean TemperatureC'].mean()) + " " + u'\u2103.')
The two weeks at the end of June and beginning ofJuly look like the best bet overall, with very little rain (9 totally dry days and only 1 day with significant rainfall (~5 mm on 5th July). Temperatures are also the best of the summer, with an average (mean) temperature of about 16 °C (see calculation above) and five days where the temperatures are between 18 °C and 22 °C.
The graphs have shown the volatility of a British summer, but a couple of weeks were found when the weather wasn't too bad in Chester in 2015. Of course this is no guarantee that the weather pattern will repeat itself in future years. To make a sensible prediction we would need to analyse the summers for many more years.
print("\nPandas version", pandas.__version__, end="")
from IPython.display import HTML
HTML('''<script>
function toggler() {
$('div.input').toggle();
location.href="#Bottom";
}
</script>
<p style="display:inline;"><center>Click <a href="javascript:toggler();">here</a> to toggle code visibility on/off</center></p>
<script>
$('div.input').show();
location.href="#Top";</script></div>''')