← Back to the chapter · Previous detail · Next detail →
Technical detail · Imputation and Noise Filtering
Estimate a seasonal pattern
def seasonal_fill(data, period=24):
"""
Fill missing values using seasonal pattern from historical data.
period: number of samples in one cycle (e.g., 24 for hourly data with daily cycle)
"""
import numpy as np
data = np.array(data, dtype=float)
n = len(data)
# Calculate seasonal pattern from valid data
seasonal = np.zeros(period)
counts = np.zeros(period)
for i, val in enumerate(data):
if not np.isnan(val):
seasonal[i % period] += val
counts[i % period] += 1
# Average seasonal values
with np.errstate(divide='ignore', invalid='ignore'):
seasonal = np.where(counts > 0, seasonal / counts, np.nan)
# Fill missing with seasonal pattern
filled = data.copy()
for i in range(n):
if np.isnan(filled[i]) and not np.isnan(seasonal[i % period]):
filled[i] = seasonal[i % period]
return filled