text_plain_output_1.png
from dateutil.relativedelta import relativedelta

from geopy.distance import distance

from statsmodels.tsa.stattools import adfuller

import matplotlib.pyplot as plt

import pandas as pd

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)

dublin_air_loc = (float(stations[stations['Station name'] == 'dublin_airport']['latitude_dd']), float(stations[stations['Station name'] == 'dublin_airport']['longitude_dd']))
stations['dist_to_dublin_air_km'] = stations.apply(lambda row: round(distance(dublin_air_loc, (row['latitude_dd'], row['longitude_dd'])).km, 2), axis=1)
stations.sort_values(by=['dist_to_dublin_air_km']).head(10)