liveBook · Manning

Seasonality effect

As explained in Outlier Detection in Python

A pattern of variation that recurs at a regular seasonal or calendar-related period.

A seasonality effect is a repeating pattern tied to a regular period, often a calendar interval such as a weekday, month position, or time of year. Unlike cycles, seasonal patterns are more regular and need not be annual. section 17.2.3

Seasonality changes what counts as unusual. A spike occurring every 10 seconds is expected, while its absence may be anomalous. For calendar data, compare observations with matching recent weekdays, month positions, or months from prior years. section 17.2.3

Two practical ways to account for seasonality are:

  • Decompose the series into trend, seasonal pattern, and residuals, then assess anomalies in the residuals after larger regular effects are removed. section 17.2.3
  • Reshape strongly seasonal data so each row represents one period and its features represent positions within that period; then apply multivariate tabular outlier detection. section 17.2.8
Listing 17.4 Using statsmodels for time-series decomposition
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from statsmodels.tsa.seasonal import seasonal_decompose
 
np.random.seed(0)
n_points = 2000
dates = np.array('2005-01-01', dtype=np.datetime64) + np.arange(n_points)
data = 12*np.sin(4 * np.pi * np.arange(n_points)/365) + \
        50.0*np.array(range(n_points))/n_points + \
        np.random.normal(12, 2, n_points)
df = pd.DataFrame({'Values': data}, index=dates)
 
result = seasonal_decompose(df, model='additive', period=365)
result.plot()
plt.show()

Inspect a Python decomposition using statsmodels, with an additive model and a 365-point period. section 17.2.3

Listing 17.12 Creating a dataframe for each period
weekly_arr = []
for i in range(0, len(df), 7):                          #1
    week_df = df.iloc[i: i+7]
    weekly_arr.append([i] + week_df['Values'].values.tolist())
weekly_df = pd.DataFrame(
weekly_arr, 
columns=['Week', 'Mon', 'Tues', 'Wed', 'Thur', 
  'Fri', 'Sat', 'Sun'])
weekly_df = weekly_df.dropna()
weekly_df

Inspect a weekly dataframe that converts seven positions in each week into separate features for tabular detection. section 17.2.8

How Outlier Detection in Python covers it

How deep it goes
Explains calendar regularity, contextual comparisons, decomposition, residuals, and period reshaping, but does not develop detector evaluation.
Examples
Uses recurring ten-second spikes, calendar sales comparisons, synthetic sine data, and weekly rows with seven daily features.
What it assumes
Assumes an ordered time series and identifiable periods while teaching how regular variation differs from less-regular cycles.
What you can do after
Diagnose/debug unusual observations by removing expected trend and seasonal effects or comparing complete periods as tabular records.
How it connects
Connects seasonality with time-series analysis, cycles, trends, residuals, time-series decomposition, and tabular outlier detection.

Also explained in

Compare how 8 books explain seasonality effect →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Outlier Detection in Python. The section links point to the parts of the book it draws on.