SharedWeek_2_project-Vesela Kovacheva.ipynbOpen in CoCalc

Project 2: Holiday weather

by Vesela Kovacheva, 12 January 2017

This is the project notebook for Week 2 of The Open University's Learn to code for Data Analysis course.

I moved to Moscow 4 months ago and so far experienced an icredibly cold and most of the time rainy weather. As we all now Russia in general is considered as a very cold country and since is already winter I decided to use the historic weather data from the Weather Underground for Moscow to try to predict two good weather weeks in winter in order to do some sightseeing without freezing during it. Of course the weather in the winter of 2017 may be very different to 2014 but it should give me some indication of when would be a good time to make a trip in the center of Moscow.

Getting the data

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):

http://www.wunderground.com/history

When the new page opens start typing 'Moscow' in the 'Location' input box and when the pop up menu comes up with the option 'Moscow, Russia Federation' select it and then click on 'Submit'.

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.

The data can be obtained month by month in CSV format. For example, the January 2014 data is available at this URL:

https://www.wunderground.com/history/airport/LHR/2014/1/1/MonthlyHistory.html?format=1

where LHR is the airport code. Changing the first 1 to 2, 3, etc. will show instead the data for February, March, etc. You can also change the 2014 in the URL to another year.

The date column heading will be the timezone, e.g. 'GMT' (Greenwich Mean Time) for Moscow, or the time offset, e.g. '+02'. It may even change throughout the year. For example, Delhi (aiport code DEL) is '+0430' from March to August and '+0330' in the other months.

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.

Weather Underground has changed in the past the way it provides data and may do so again in the future. I have therefore collated the whole 2014 data in the provided 'Moscow_SVO_2014.csv' file.

Now load the CSV file into a dataframe making sure that any extra spaces are skipped:

import warnings
warnings.simplefilter('ignore', FutureWarning)

from pandas import *
moscow = read_csv('Moscow_SVO_2014.csv', skipinitialspace=True)

Cleaning the data

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'.

moscow = moscow.rename(columns={'WindDirDegrees<br />' : 'WindDirDegrees'})

remove the < br /> html line breaks from the values in the 'WindDirDegrees' column.

moscow['WindDirDegrees'] = moscow['WindDirDegrees'].str.rstrip('<br />')

and change the values in the 'WindDirDegrees' column to float64:

moscow['WindDirDegrees'] = moscow['WindDirDegrees'].astype('float64')   

We definitely need to change the values in the 'GMT' column into values of the datetime64 date type.

moscow['Date'] = to_datetime(moscow['Date'])

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:

moscow.index = moscow['Date']

Finding a week with good weather in winter to do a city trip

According to meteorologists, winter extends for the whole months of December, January, February in the northern hemisphere. Since it is already January im going to create a dataframe that holds just the months January an February using the datetime index, like this:

winter = moscow.ix[datetime(2014,1,1) : datetime(2014,2,28)]

I now look for the days with temperature above 0 Celsius.

winter[winter['Mean TemperatureC'] >= 0]
Date Max TemperatureC Mean TemperatureC Min TemperatureC Dew PointC MeanDew PointC Min DewpointC Max Humidity Mean Humidity Min Humidity ... Max VisibilityKm Mean VisibilityKm Min VisibilitykM Max Wind SpeedKm/h Mean Wind SpeedKm/h Max Gust SpeedKm/h Precipitationmm CloudCover Events WindDirDegrees
Date
2014-01-05 2014-01-05 1 0 -1 1 -1 -3 100 93 81 ... 10.0 6.0 1.0 26 16 NaN 0.0 8.0 Snow 195.0
2014-01-06 2014-01-06 1 0 0 1 0 -1 100 95 93 ... 10.0 3.0 0.0 14 8 26.0 0.0 8.0 Fog-Snow 130.0
2014-01-07 2014-01-07 2 1 0 1 1 0 100 100 93 ... 5.0 2.0 0.0 14 11 NaN 0.0 8.0 Fog 124.0
2014-01-08 2014-01-08 3 2 1 2 2 1 100 98 93 ... 10.0 6.0 3.0 29 19 40.0 0.0 8.0 Rain-Snow 211.0
2014-01-09 2014-01-09 3 2 2 2 2 1 100 98 93 ... 10.0 3.0 0.0 21 16 NaN 0.0 8.0 Fog-Rain 225.0
2014-01-10 2014-01-10 4 2 2 3 2 1 100 94 87 ... 10.0 9.0 3.0 26 18 40.0 0.0 8.0 Rain 184.0
2014-01-11 2014-01-11 4 0 -3 3 0 -5 100 93 86 ... 10.0 8.0 2.0 35 18 50.0 0.0 8.0 Rain-Snow 236.0
2014-02-09 2014-02-09 1 0 -1 -1 -1 -3 100 91 86 ... 10.0 6.0 2.0 21 13 35.0 0.0 8.0 Snow 145.0
2014-02-13 2014-02-13 2 1 1 2 0 -1 100 93 87 ... 10.0 7.0 1.0 21 11 32.0 0.0 8.0 Fog-Rain 156.0
2014-02-14 2014-02-14 4 2 1 3 2 1 100 98 93 ... 5.0 2.0 0.0 18 6 32.0 0.0 8.0 Fog 151.0
2014-02-15 2014-02-15 3 2 1 2 1 0 100 97 93 ... 10.0 7.0 0.0 18 11 32.0 0.0 8.0 Fog-Rain 205.0
2014-02-16 2014-02-16 2 1 1 1 -1 -2 100 91 75 ... 10.0 7.0 1.0 14 10 NaN 0.0 8.0 Fog 198.0
2014-02-17 2014-02-17 1 0 0 1 0 -1 100 95 87 ... 10.0 6.0 1.0 21 16 NaN 0.0 8.0 Snow 207.0
2014-02-18 2014-02-18 2 1 1 1 0 -1 93 91 87 ... 10.0 9.0 3.0 26 18 NaN 0.0 8.0 Rain-Snow 235.0
2014-02-23 2014-02-23 2 0 -1 2 1 -2 100 96 93 ... 10.0 5.0 1.0 21 16 NaN 0.0 8.0 Rain-Snow 195.0
2014-02-24 2014-02-24 2 1 1 1 0 -1 100 90 81 ... 10.0 9.0 1.0 21 14 NaN 0.0 8.0 Fog 217.0
2014-02-25 2014-02-25 3 1 -1 0 -4 -8 93 71 45 ... 10.0 9.0 7.0 18 13 35.0 0.0 6.0 NaN 196.0

17 rows × 23 columns

Winter 2014 as we can see was warm in Moscow: there are more than 10 days with temperature above 0 Celsius. 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:

winter['Mean TemperatureC'].plot(grid=True, figsize=(10,5))
<matplotlib.axes._subplots.AxesSubplot at 0x7faf8d2762b0>

Well looking at the graph February looks good for mean temperatures over 0 degrees C so let's also put precipitation on the graph too:

winter[['Mean TemperatureC', 'Precipitationmm']].plot(grid=True, figsize=(10,5))
<matplotlib.axes._subplots.AxesSubplot at 0x7faf8d37ce10>

February is still looking good so let's have a closer look by just plotting mean temperature and precipitation for February.

february = winter.ix[datetime(2014,2,1) : datetime(2014,2,28)]
february[['Mean TemperatureC', 'Precipitationmm']].plot(grid=True, figsize=(10,5))
<matplotlib.axes._subplots.AxesSubplot at 0x7faf8d17e160>

The middle of Fabruary looks pretty good with 5 days of temperature above 0 degrees-from 12th until 16th.

Conclusions

The graphs have shown the volatility of a Russian winter, but a couple of weeks were found when the weather wasn't too bad in 2014. 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 winters for many more years.