text_plain_output_1.png
from dateutil.relativedelta import relativedelta

from statsmodels.tsa.stattools import adfuller

import json

import matplotlib.pyplot as plt

import pandas as pd

import plotly.graph_objs as go

import json
import plotly.graph_objs as go
import urllib.request
def read_geojson(url):
    with urllib.request.urlopen(url) as url:
        jdata = json.loads(url.read().decode())
    return jdata

from dateutil.relativedelta import relativedelta

# rolling averages and std
def rolling_stat(timeseries, window_size):
    # Determing rolling statistics
    rolmean = timeseries.rolling(window = window_size).mean()
    rolstd = timeseries.rolling(window = window_size).std()

    # Plot rolling statistics:
    fig, ax = plt.subplots(figsize = (12, 4))
    orig = plt.plot(timeseries, color = '#4DBEEE', label = 'Original')
    std = plt.plot(rolstd, color = 'black', label = 'Rolling Std')
    mean = plt.plot(rolmean, color = 'red', label = 'Rolling Mean')
    plt.legend(loc = 'best')
    plt.title('Rolling Mean and Standard Deviation')
    plt.grid()
    plt.show(block=False)

# get n predictions for series by model
def future_preds_df(model, series, num_steps):
    pred_first = series.index.max() + relativedelta(weeks = 1)
    pred_last = series.index.max() + relativedelta(weeks = num_steps)
    date_range_index = pd.date_range(pred_first, pred_last, freq = 'W')
    vals = model.predict(n_periods = num_steps)
    return pd.DataFrame(vals,index = date_range_index)

# Augmented Dicky-Fuller Test
def adf_test(timeseries):
    adf, pvalue, usedlag, nobs, critical_values, icbest = adfuller(timeseries)
    print("Test statistic: ", adf, 2)
    print("P-value: ", pvalue)
    print("Critical values: ", critical_values)
    
# source: notebook 06f_DEMO_SARIMA_Prophet by IBM Specialized Models: Time Series and Survival Analysis course

stations = pd.read_csv('../input/hourly-weather-data-in-ireland-from-24-stations/station_list.csv', index_col=0)

ireland_url = 'https://gist.githubusercontent.com/pnewall/9a122c05ba2865c3a58f15008548fbbd/raw/5bb4f84d918b871ee0e8b99f60dde976bb711d7c/ireland_counties.geojson'
jdata = read_geojson(ireland_url)
jdata['type']

county_names = [jdata['features'][i]['id'] for i in range(len(jdata['features']))]
colorscale = [[0.0, '#cccba1'], [0.5, '#a1ccaa'], [1.0, '#a1c1cc']]
trace1 = go.Choropleth(geojson=jdata, showscale=False, colorscale=colorscale, zmin=0, zmax=1, z=[0.5] * len(jdata['features']), locations=county_names, featureidkey='properties.name')
trace2 = go.Scattergeo(lon=stations['longitude_dd'], lat=stations['latitude_dd'], text=stations['Station name'], mode='markers', marker=dict(opacity=0.8, color='blue', reversescale=True, autocolorscale=False, line=dict(width=0.5, color='lightgray')))
fig = go.Figure(data=[trace1, trace2])
fig.update_geos(center=dict(lon=-7.5, lat=53.7), lataxis_range=[51, 56], lonaxis_range=[-13, -6], resolution=50, scope='europe')
fig.update_layout(height=500, margin={'r': 0, 't': 50, 'l': 0, 'b': 0}, title='Locations of weather stations in Ireland')
fig.show()