36  seasonal decomposition from scratch

We will work together, running each cell in this Notebook step by step. Start by downloading the notebook (“Code” button above).

The data comes from Climate Reanalyzer, and the notebook downloads the most recent version every time you run it, so your graphs will include days that are not on this page. If the download doesn’t work (no internet, or the site is down), the notebook falls back to local copies of the data. Download these 2 csv files and put them in the same folder as the notebook:

36.1 sea surface temperature

import stuff
import numpy as np
import matplotlib.pyplot as plt
import matplotlib as mpl
import pandas as pd
from pandas.plotting import register_matplotlib_converters
register_matplotlib_converters()  # datetime converter for a matplotlib
import seaborn as sns
sns.set_theme(style="ticks", font_scale=1.5)
from statsmodels.tsa.seasonal import seasonal_decompose
import matplotlib.dates as mdates
from matplotlib.dates import DateFormatter
from datetime import datetime as dt
import time
from statsmodels.tsa.stattools import adfuller

# %matplotlib widget
download and plot data
import urllib.request
import json

def download_sst(region):
    """download daily SST from Climate Reanalyzer, return a dataframe with an 'sst' column"""
    url = f"https://climatereanalyzer.org/clim/sst_daily/json_2clim/oisst2.1_{region}_sst_day.json"
    with urllib.request.urlopen(url, timeout=30) as response:
        data = json.load(response)
    # data is a list with one entry per year, each holding 366 daily values starting on Jan 1
    pieces = []
    for entry in data:
        if not entry['name'].isdigit():  # skip "Preliminary", "1982-2010" climatology, etc.
            continue
        year = int(entry['name'])
        dates = pd.date_range(f"{year}-01-01", f"{year}-12-31", freq="D")  # 365 or 366 days
        pieces.append(pd.Series(entry['data'][:len(dates)], index=dates, dtype=float))
    sst = pd.concat(pieces).dropna()  # days not measured (yet) are empty
    return sst.rename_axis('date').to_frame('sst')

try:
    df_world = download_sst("world2")
    df_north = download_sst("natlan")
    print("downloaded data up to", df_world.index[-1].date())
except Exception as error:
    print("download failed, reading local csv files instead:", error)
    df_world = pd.read_csv("sst_world.csv", index_col='date', parse_dates=True)
    df_north = pd.read_csv("sst_north.csv", index_col='date', parse_dates=True)

fig, ax = plt.subplots(2, 1, figsize=(8,5), sharex=True)
fig.subplots_adjust(hspace=0.1)  # increase vertical space between panels
ax[0].plot(df_world['sst'], color="black")
ax[1].plot(df_north['sst'], color="black")
fig.text(0.02, 0.5, 'sea surface temperature (°C)', va='center', rotation='vertical')
ax[1].set(yticks=[20, 25])
ax[0].text(0.5, 0.97, r"world average", transform=ax[0].transAxes,
           horizontalalignment='center', verticalalignment='top',)
ax[1].text(0.5, 0.97, r"north atlantic", transform=ax[1].transAxes,
           horizontalalignment='center', verticalalignment='top',)
pass
downloaded data up to 2026-11-07

36.2 trend

Take a rolling average of the data, using a 365-day window. Do it twice, one using '365D' and another using 365. What’s the difference?

df_north['trend'] = df_north['sst'].rolling(365, center=True).mean()
df_north['trend2'] = df_north['sst'].rolling('365D', center=True).mean()
df_world['trend'] = df_world['sst'].rolling(365, center=True).mean()
df_world['trend2'] = df_world['sst'].rolling('365D', center=True).mean()
plot trend
fig, ax = plt.subplots(2, 1, figsize=(8,5), sharex=True)
fig.subplots_adjust(hspace=0.1)  # increase vertical space between panels
ax[0].plot(df_world['sst'], color="black", label="daily")
ax[0].plot(df_world['trend2'], color="xkcd:cerulean", label="'365D' mean")
ax[0].plot(df_world['trend'], color="xkcd:hot pink", label="365-points mean")
ax[1].plot(df_north['sst'], color="black")
ax[1].plot(df_north['trend2'], color="xkcd:cerulean")
ax[1].plot(df_north['trend'], color="xkcd:hot pink")
fig.text(0.02, 0.5, 'sea surface temperature (°C)', va='center', rotation='vertical')
ax[0].set(title="trend  = 365-day rolling mean")
ax[1].set(yticks=[20, 25])
ax[0].text(0.5, 0.97, r"world average", transform=ax[0].transAxes,
           horizontalalignment='center', verticalalignment='top',)
ax[1].text(0.5, 0.97, r"north atlantic", transform=ax[1].transAxes,
           horizontalalignment='center', verticalalignment='top',)
ax[0].legend(frameon=False)
pass

36.3 detrend

\text{detrended} = \text{signal} - \text{trend}

df_north['detrended'] = df_north['sst'] - df_north['trend']
df_world['detrended'] = df_world['sst'] - df_world['trend']
plot trend
fig, ax = plt.subplots(2, 1, figsize=(8,5), sharex=True)
fig.subplots_adjust(hspace=0.1)  # increase vertical space between panels
ax[0].plot(df_world['detrended'])
ax[1].plot(df_north['detrended'])
fig.text(0.02, 0.5, 'detrended sst temperature (°C)', va='center', rotation='vertical')
ax[0].set(ylim=[-0.4,0.4],
          title="detrended")
ax[1].set(ylim=[-4,4],)
ax[0].text(0.5, 0.97, r"world average", transform=ax[0].transAxes,
           horizontalalignment='center', verticalalignment='top',)
ax[1].text(0.5, 0.97, r"north atlantic", transform=ax[1].transAxes,
           horizontalalignment='center', verticalalignment='top',)
pass

36.4 seasonal component

It is useful to add two new columns to our dataframes: doy (day of year) and year:

df_north['doy'] = df_north.index.day_of_year
df_north['year'] = df_north.index.year
df_world['doy'] = df_world.index.day_of_year
df_world['year'] = df_world.index.year
df_world
sst trend trend2 detrended doy year
date
1981-09-01 20.147 NaN 20.007077 NaN 244 1981
1981-09-02 20.138 NaN 20.008065 NaN 245 1981
1981-09-03 20.133 NaN 20.009049 NaN 246 1981
1981-09-04 20.130 NaN 20.009946 NaN 247 1981
1981-09-05 20.119 NaN 20.010781 NaN 248 1981
... ... ... ... ... ... ...
2026-11-03 21.022 NaN 21.057690 NaN 307 2026
2026-11-04 21.032 NaN 21.057618 NaN 308 2026
2026-11-05 21.037 NaN 21.057616 NaN 309 2026
2026-11-06 21.037 NaN 21.057679 NaN 310 2026
2026-11-07 21.032 NaN 21.057820 NaN 311 2026

16504 rows × 6 columns

36.4.1 group by

This is an extremely useful method. As the name suggests, it groups data according to some criterion.

gb_year_north = df_north.groupby('year')
gb_year_world = df_world.groupby('year')

Just like resample and rolling, groupby doesn’t do anything after grouping the data, it waits for further instructions.

gb_year_world['sst'].mean()
year
1981    19.953533
1982    20.051666
1983    20.133049
1984    20.029951
1985    19.923997
1986    19.994058
1987    20.148948
1988    20.107847
1989    20.039701
1990    20.152912
1991    20.145792
1992    20.069243
1993    20.080611
1994    20.098230
1995    20.170759
1996    20.118675
1997    20.269997
1998    20.328770
1999    20.117570
2000    20.151910
2001    20.286447
2002    20.321718
2003    20.371005
2004    20.328891
2005    20.412482
2006    20.374159
2007    20.321000
2008    20.313038
2009    20.432312
2010    20.436249
2011    20.314384
2012    20.369612
2013    20.404005
2014    20.525603
2015    20.647521
2016    20.671123
2017    20.607729
2018    20.567978
2019    20.652666
2020    20.678068
2021    20.630775
2022    20.648411
2023    20.889337
2024    20.940407
2025    20.818030
2026    21.030768
Name: sst, dtype: float64

If all you need is the average by years, then the most common way of writing the command would be in one single line:

df_world.groupby('year')['sst'].mean()

Let’s use groupby to plot world SST for all years, see how handy this method is. Also note that groupby return an object that can be iterated upon, as we do below.

fig, ax = plt.subplots()
for year, data in gb_year_world:
    ax.plot(data['doy'], data['sst'], color="black")
ax.set(xlabel="DOY", ylabel="SST");

With a few more lines of code we can make this look pretty.

Show the code
fig, ax = plt.subplots(2, 1, figsize=(8, 5), sharex=True)
fig.subplots_adjust(hspace=0.1)

# define the segment of the colormap to use
start, end = 0.3, 0.8
base_cmap = plt.cm.hot_r
new_colors = base_cmap(np.linspace(start, end, 256))
new_cmap = mpl.colors.LinearSegmentedColormap.from_list("trunc({n},{a:.2f},{b:.2f})".format(n=base_cmap.name, a=start, b=end), new_colors)
# defining the years for plotting
years = [year for year, _ in gb_year_world] + [year for year, _ in gb_year_north]
min_year, max_year = min(years), max(years)
# create a new normalization that matches the years to the new colormap
norm = mpl.colors.Normalize(vmin=min_year, vmax=max_year)
# plotting the data with year-specific colors from the new colormap
for year, data in gb_year_world:
    ax[0].plot(data.index.day_of_year, data['sst'], color=new_cmap(norm(year)))
for year, data in gb_year_north:
    ax[1].plot(data.index.day_of_year, data['sst'], color=new_cmap(norm(year)))

# colorbar setup
sm = mpl.cm.ScalarMappable(cmap=new_cmap, norm=norm)
sm.set_array([])
cbar = fig.colorbar(sm, ax=ax.ravel().tolist(), orientation='vertical', fraction=0.046, pad=0.04)
ticks_years = [1981, 1991, 2001, 2011, max_year]  # Specify years for colorbar ticks
cbar.set_ticks(ticks_years)
cbar.set_ticklabels(ticks_years)
# adding shared ylabel and xlabel
fig.text(0.02, 0.5, 'SST temperature (°C)', va='center', rotation='vertical')
ax[1].set_xlabel("day of year")
ax[0].text(0.97, 0.97, r"world average", transform=ax[0].transAxes,
           horizontalalignment='right', verticalalignment='top',)
ax[1].text(0.97, 0.97, r"north atlantic", transform=ax[1].transAxes,
           horizontalalignment='right', verticalalignment='top',);

Now let’s do the same for the detrended SST:

Show the code
fig, ax = plt.subplots(2, 1, figsize=(8, 5), sharex=True)
fig.subplots_adjust(hspace=0.1)

# define the segment of the colormap to use
start, end = 0.3, 0.8
base_cmap = plt.cm.hot_r
new_colors = base_cmap(np.linspace(start, end, 256))
new_cmap = mpl.colors.LinearSegmentedColormap.from_list("trunc({n},{a:.2f},{b:.2f})".format(n=base_cmap.name, a=start, b=end), new_colors)
# defining the years for plotting
years = [year for year, _ in gb_year_world] + [year for year, _ in gb_year_north]
min_year, max_year = min(years), max(years)
# create a new normalization that matches the years to the new colormap
norm = mpl.colors.Normalize(vmin=min_year, vmax=max_year)
# plotting the data with year-specific colors from the new colormap
for year, data in gb_year_world:
    ax[0].plot(data.index.day_of_year, data['detrended'], color=new_cmap(norm(year)))
for year, data in gb_year_north:
    ax[1].plot(data.index.day_of_year, data['detrended'], color=new_cmap(norm(year)))
# colorbar setup
sm = mpl.cm.ScalarMappable(cmap=new_cmap, norm=norm)
sm.set_array([])
cbar = fig.colorbar(sm, ax=ax.ravel().tolist(), orientation='vertical', fraction=0.046, pad=0.04)
ticks_years = [1981, 1991, 2001, 2011, max_year]  # Specify years for colorbar ticks
cbar.set_ticks(ticks_years)
cbar.set_ticklabels(ticks_years)
# adding shared ylabel and xlabel
fig.text(0.02, 0.5, 'detrended SST temperature (°C)', va='center', rotation='vertical')
ax[0].set(yticks=[-0.4,0,0.4])
ax[1].set_xlabel("day of year")
ax[0].text(0.97, 0.97, r"world average", transform=ax[0].transAxes,
           horizontalalignment='right', verticalalignment='top',)
ax[1].text(0.97, 0.97, r"north atlantic", transform=ax[1].transAxes,
           horizontalalignment='right', verticalalignment='top',);

In order to determine what is the seasonal component, we need to calculate the average of the detrended data across all years. Again, groupby comes to rescue in a most elegant way:

avg_north = df_north.groupby('doy')['detrended'].mean()
avg_world = df_world.groupby('doy')['detrended'].mean()

Leap years introduce a small mismatch here: from March onwards, the same calendar date has a day of year one larger in leap years (March 1 is day 61 instead of 60), and day 366 exists only in leap years, so its average comes from just a handful of years; this is negligible for a slowly varying quantity like SST, but worth handling for data with sharp events tied to the calendar.

Let’s incorporate the averages in the same graphs we saw above:

Show the code
fig, ax = plt.subplots(2, 1, figsize=(8, 5), sharex=True)
fig.subplots_adjust(hspace=0.1)

# define the segment of the colormap to use
start, end = 0.3, 0.8
base_cmap = plt.cm.hot_r
new_colors = base_cmap(np.linspace(start, end, 256))
new_cmap = mpl.colors.LinearSegmentedColormap.from_list("trunc({n},{a:.2f},{b:.2f})".format(n=base_cmap.name, a=start, b=end), new_colors)
# defining the years for plotting
years = [year for year, _ in gb_year_world] + [year for year, _ in gb_year_north]
min_year, max_year = min(years), max(years)
# create a new normalization that matches the years to the new colormap
norm = mpl.colors.Normalize(vmin=min_year, vmax=max_year)
# plotting the data with year-specific colors from the new colormap
for year, data in gb_year_world:
    ax[0].plot(data.index.day_of_year, data['detrended'], color=new_cmap(norm(year)))
for year, data in gb_year_north:
    ax[1].plot(data.index.day_of_year, data['detrended'], color=new_cmap(norm(year)))

ax[0].plot(avg_world, color='dodgerblue', lw=2, ls='--')
ax[1].plot(avg_north, color='dodgerblue', lw=2, ls='--', label="mean across years")
ax[1].legend(loc="upper left", frameon=False)

# colorbar setup
sm = mpl.cm.ScalarMappable(cmap=new_cmap, norm=norm)
sm.set_array([])
cbar = fig.colorbar(sm, ax=ax.ravel().tolist(), orientation='vertical', fraction=0.046, pad=0.04)
ticks_years = [1981, 1991, 2001, 2011, max_year]  # Specify years for colorbar ticks
cbar.set_ticks(ticks_years)
cbar.set_ticklabels(ticks_years)
# adding shared ylabel and xlabel
fig.text(0.02, 0.5, 'detrended SST temperature (°C)', va='center', rotation='vertical')
ax[0].set(yticks=[-0.4,0,0.4])
ax[1].set_xlabel("day of year")
ax[0].text(0.97, 0.97, r"world average", transform=ax[0].transAxes,
           horizontalalignment='right', verticalalignment='top',)
ax[1].text(0.97, 0.97, r"north atlantic", transform=ax[1].transAxes,
           horizontalalignment='right', verticalalignment='top',);

Show the code
avg_north
doy
1     -1.251150
2     -1.277675
3     -1.303238
4     -1.336716
5     -1.370651
         ...   
362   -1.120113
363   -1.152127
364   -1.184421
365   -1.214033
366   -1.225824
Name: detrended, Length: 366, dtype: float64
Show the code
df_north
sst trend trend2 detrended doy year
date
1981-09-01 23.783 NaN 21.237175 NaN 244 1981
1981-09-02 23.774 NaN 21.225130 NaN 245 1981
1981-09-03 23.795 NaN 21.213097 NaN 246 1981
1981-09-04 23.831 NaN 21.200962 NaN 247 1981
1981-09-05 23.866 NaN 21.188583 NaN 248 1981
... ... ... ... ... ... ...
2026-11-03 23.361 NaN 23.552909 NaN 307 2026
2026-11-04 23.330 NaN 23.568312 NaN 308 2026
2026-11-05 23.312 NaN 23.583686 NaN 309 2026
2026-11-06 23.295 NaN 23.599049 NaN 310 2026
2026-11-07 23.272 NaN 23.614388 NaN 311 2026

16504 rows × 6 columns

The seasonal component is the average across all years, repeated over and over.

df_north['seasonal'] = df_north['doy'].map(avg_north)
df_world['seasonal'] = df_world['doy'].map(avg_world)
Show the code
fig, ax = plt.subplots(2, 1, figsize=(8, 5), sharex=True)
fig.subplots_adjust(hspace=0.1)
ax[0].plot(df_world.loc['2000':'2004', 'seasonal'])
ax[1].plot(df_north.loc['2000':'2004', 'seasonal'])
# adding shared ylabel and xlabel
fig.text(0.02, 0.5, 'seasonal component (°C)', va='center', rotation='vertical')
ax[0].text(0.97, 0.97, r"world average", transform=ax[0].transAxes,
           horizontalalignment='right', verticalalignment='top',)
ax[1].text(0.97, 0.97, r"north atlantic", transform=ax[1].transAxes,
           horizontalalignment='right', verticalalignment='top',);

36.5 residual

\text{residual} = \text{signal} - \text{trend} - \text{seasonal}

df_world['resid'] = df_world['detrended'] - df_world['seasonal']
df_north['resid'] = df_north['detrended'] - df_north['seasonal']
Show the code
fig, ax = plt.subplots(2, 1, figsize=(8, 5), sharex=True)
fig.subplots_adjust(hspace=0.1)
ax[0].plot(df_world['resid'])
ax[1].plot(df_north['resid'])
# adding shared ylabel and xlabel
fig.text(0.02, 0.5, 'residual (°C)', va='center', rotation='vertical')
ax[0].text(0.97, 0.97, r"world average", transform=ax[0].transAxes,
           horizontalalignment='right', verticalalignment='top',)
ax[1].text(0.97, 0.97, r"north atlantic", transform=ax[1].transAxes,
           horizontalalignment='right', verticalalignment='top',);

How do we know we have properly decomposed our signal into trend, seasonal and residual? The residual should be stationary. Let’s check using the ADF test:

Show the code
alpha = 0.05  # significance level

for name, df in [("World-average", df_world), ("North-Atlantic", df_north)]:
    result = adfuller(df['resid'].dropna())
    p_value = result[1]
    print(f"{name} residual")
    print(f"ADF test p-value: {p_value:.2e}")
    if p_value < alpha:
        print(f"p < {alpha}: reject the null hypothesis, the residual is stationary\n")
    else:
        print(f"p >= {alpha}: cannot reject the null hypothesis, the residual may be non-stationary\n")
World-average residual
ADF test p-value: 4.57e-26
p < 0.05: reject the null hypothesis, the residual is stationary

North-Atlantic residual
ADF test p-value: 8.10e-24
p < 0.05: reject the null hypothesis, the residual is stationary

Both residuals are stationary according to ADF. But be careful about what this means. The ADF test looks for one specific kind of non-stationarity: a unit root, the kind of wandering behavior of a random walk. A regular annual cycle is not a unit root, so the ADF test also says “stationary” for a series that still has the whole seasonal cycle in it, and even for the raw data:

ADF p-values for raw, detrended and residual
adf_table = {}
for name, df in [("world average", df_world), ("north atlantic", df_north)]:
    adf_table[name] = {column: adfuller(df[column].dropna())[1]
                       for column in ["sst", "detrended", "resid"]}
pd.DataFrame(adf_table).map(lambda p: f"{p:.1e}").rename_axis("ADF p-value")
world average north atlantic
ADF p-value
sst 3.9e-08 0.0e+00
detrended 7.3e-30 0.0e+00
resid 4.6e-26 8.1e-24

So the ADF test cannot tell us whether we managed to remove the seasonal component. Here is a check that can. If the annual cycle is still in the data, then today is similar to the same day one year ago, and the autocorrelation at a lag of one year is high. If we removed the annual cycle, the autocorrelation at that lag should be close to zero.

Our data is daily and has no gaps, so a lag of one year is a lag of 365 data points (leap years shift this by a quarter of a day, which doesn’t matter here). With hourly data, one year would be a lag of 8760 points; with monthly data, a lag of 12.

autocorrelation at a lag of one year
lag = 365  # one year, because the data is daily
ac_table = {}
for name, df in [("world average", df_world), ("north atlantic", df_north)]:
    ac_table[name] = {column: df[column].autocorr(lag)
                      for column in ["detrended", "resid"]}
pd.DataFrame(ac_table).round(2).rename_axis(f"autocorrelation at lag {lag}")
world average north atlantic
autocorrelation at lag 365
detrended 0.87 1.00
resid 0.04 0.12

The detrended series, which still contains the annual cycle, is strongly correlated with itself one year later. In the residual, this correlation almost disappears: the seasonal component took the annual cycle with it.

36.6 seasonal decomposition

It is customary to plot each component in its own panel:

Show the code
fig, ax = plt.subplots(4, 1, figsize=(8,6), sharex=True)
pos = (0.5, 0.9)
components =["sst", "trend", "seasonal", "resid"]
colors = ["tab:blue", "tab:orange", "tab:green", "tab:red"]
for axx, component, color in zip(ax, components, colors):
    data = getattr(df_world, component)
    axx.plot(data, color=color)
    axx.text(*pos, component, bbox=dict(facecolor='white', alpha=0.8),
           transform=axx.transAxes, ha='center', va='top')
ax[0].set(title="world average");

Show the code
fig, ax = plt.subplots(4, 1, figsize=(8,6), sharex=True)
pos = (0.5, 0.9)
components =["sst", "trend", "seasonal", "resid"]
colors = ["tab:blue", "tab:orange", "tab:green", "tab:red"]
for axx, component, color in zip(ax, components, colors):
    data = getattr(df_north, component)
    axx.plot(data, color=color)
    axx.text(*pos, component, bbox=dict(facecolor='white', alpha=0.8),
           transform=axx.transAxes, ha='center', va='top')
ax[0].set(title="north atlantic");

Show the code
fig, ax = plt.subplots(1, 2, figsize=(10,6))
ax[0].plot(df_world['sst'], color="tab:blue", label="sst")
ax[0].plot(df_world['trend'] + df_world['resid'], color="tab:orange", label="trend+resid")
ax[0].plot(df_world['trend'] + df_world['seasonal'], color="tab:red", label="trend+seasonal")
ax[0].plot(df_world['trend'], color="black", label="trend")
ax[0].set(ylabel="SST (°C)",
          title="world average")
date_form = DateFormatter("%Y")
ax[0].xaxis.set_major_formatter(date_form)
ax[0].xaxis.set_major_locator(mdates.YearLocator(10))
ax[0].legend(frameon=False)

start = "2013-01-01"
end = "2016-01-01"
zoom = slice(start, end)
ax[1].plot(df_world.loc[zoom, 'sst'], color="tab:blue", label="sst")
ax[1].plot(df_world.loc[zoom, 'trend'] + df_world.loc[zoom, 'resid'], color="tab:orange", label="trend+resid")
ax[1].plot(df_world.loc[zoom, 'trend'] + df_world.loc[zoom, 'seasonal'], color="tab:red", label="trend+seasonal")
ax[1].plot(df_world.loc[zoom, 'trend'], color="black", label="trend")
date_form = DateFormatter("%Y")
ax[1].xaxis.set_major_formatter(date_form)
ax[1].xaxis.set_major_locator(mdates.YearLocator(1))
ax[1].set_title("components, 2013--2016");