Skip to content
Tutorials Center
My learning

Topics

Lesson 27 of 76 ยท Exploratory Data Analysis

Trends, Seasonality and Time Patterns

Explore time series data: resample to months, smooth with rolling means, split trend and seasonality with statsmodels, compare year over year and flag anomalies.

Many business questions are about change over time: Are sales growing? Is the dip in March an event or a yearly habit? Did traffic really jump last Tuesday? Time-ordered data needs its own exploration tools, because the order of rows matters and values close in time resemble each other.

A time series is usually a mix of four ingredients:

  • Trend: the slow, long-run direction (up, down, flat).
  • Seasonality: a pattern that repeats at a fixed period, such as weekly or yearly cycles.
  • Noise: random day-to-day variation.
  • Events and anomalies: one-off spikes, drops, level shifts, outages.

EDA for time series means separating these ingredients so that you can see each one. This lesson works with a synthetic daily sales series and covers resampling, rolling means, decomposition, year-over-year comparison and anomaly spotting. It continues the EDA process; the pandas side is in Time Series Basics and Resampling, and forecasting itself comes later in Forecasting Basics.

flowchart LR
    S["Observed series"] --> T["Trend<br/>slow direction"]
    S --> Z["Seasonality<br/>repeating cycle"]
    S --> N["Noise<br/>random variation"]
    S --> A["Anomalies<br/>one-off events"]

An observed series is the sum (or product) of several parts; EDA tries to pull them apart.

Build the practice data

The series below has a gentle upward trend, a weekly cycle (busier on weekends), a yearly cycle (busy in winter), random noise and three planted anomalies. The next examples all start from this same code, so you can see how each tool treats it.

Python Daily sales: look at the raw series

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Raw daily data is noisy: you can see a rough wave and a slow rise, but the details are buried. Before doing anything, run the structural checks: is the index sorted, is it a real datetime, are any days missing? A complete, regular index is what makes the tools below behave.

Resampling: changing the time scale

Resampling aggregates data to a coarser period: days to weeks or months. Averaging smooths noise, and the right period reveals different things. Weekly totals hide the day-of-week cycle; monthly totals reveal the yearly cycle. Choose the aggregation to match the meaning: sums for volumes (sales, visits), means for rates and levels (temperature, price), last value for balances.

Python
monthly = sales.resample("MS").sum()      # "MS" = month start
weekly_mean = sales.resample("W").mean()

Careful: partial periods mislead. A half-finished last month has a small total only because it is incomplete. Drop it or compare it with the same days of earlier periods.

Rolling means: smoothing a trend

A rolling mean replaces each point by the average of the surrounding window, series.rolling(window).mean(). A window of 7 days removes the weekly cycle exactly (every day of the week appears once), leaving trend and yearly shape. A 30-day window smooths more but reacts more slowly. Trade-offs:

  • A longer window smooths more but lags behind real changes.
  • By default the window looks backward, so the smoothed line is a little late. center=True aligns it with the middle of the window.
  • The first window - 1 values are NaN.
Python Seasonal pattern by weekday, and rolling means

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

The weekday profile shows Saturday and Sunday about 25 higher than the weekdays (roughly 258 against 233). The 7-day mean removes that cycle; the 30-day mean also removes most of the noise, leaving the yearly wave on a rising baseline. The monthly means make the same point with fewer points.

Decomposition: trend, seasonality and remainder

Seasonal decomposition estimates the pieces explicitly. statsmodels.tsa.seasonal.seasonal_decompose splits a series into trend, seasonal and residual parts for a period you give it (12 for monthly data with a yearly cycle, 7 for daily data with a weekly one). Two forms exist:

  • additive: observed = trend + seasonal + residual, when the seasonal swing is about the same size at any level;
  • multiplicative: observed = trend x seasonal x residual, when the swing grows with the level.

The residual is what is left; large residuals are candidates for anomalies. At least two full cycles of data are required.

Python Decompose monthly sales

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Four panels appear: the observed monthly series, the trend, the repeating seasonal shape and the residual. The seasonal values show a winter high and a summer low, and the trend climbs steadily. Note the trend line starts and ends half a year in from the edges: the centered moving average needs data on both sides.

Year-over-year comparison

To compare with the same period last year (YoY), pivot the data so that rows are months and columns are years. This removes seasonality from the comparison: January is compared with January. Report the percentage change, and always look at the absolute numbers as well, since a +50% change from a tiny base is not news.

Python Year-over-year growth

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Each column after the first shows growth against the previous year: full-year totals grow by about 11 percent in 2024 and about 9 percent in 2025, with single months ranging from roughly 4 to 14 percent. When you see the same sign in every month, the pattern is a trend; if only one month stands out, look for an event.

Anomalies in time

An anomaly is a point that does not fit the pattern. Compare each value with what you would expect, then flag large deviations. A global rule such as "more than 3 standard deviations from the mean" fails here, because the expected level changes with season and trend. A better approach: remove the expected pattern first (rolling median, or a decomposition's residual), then look at the leftover.

Python Find anomalies with a rolling baseline

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

The three planted events (a flash sale, an outage, a bulk order) are found because each is far from its neighbors of the same kind. A flagged point is a question, not a verdict: check logs, calendars and campaigns before treating it as an error. A flash sale is a real event you may want to keep and model; an outage might be better replaced by a missing value. See Outliers, Errors and Impossible Values for the decision.

Pitfalls specific to time data

  • Time zones and daylight saving shift timestamps and duplicate or drop an hour; keep timestamps in UTC when possible.
  • Calendar effects: holidays move (Easter), months have different lengths. Compare per-day averages if month lengths matter.
  • Smoothing with future data: a centered rolling mean uses values after the date. That is fine for EDA but is leakage if used as a feature to predict the future.
  • Mistaking seasonality for trend: compare the same period across years before declaring growth.
  • Spurious correlation: two series that both trend upward correlate strongly even when unrelated. Compare changes or detrended values instead.

Check your understanding

Quick check ยท 5 questions

of 5 answered You scored / 5 ยท perfect!

  1. 1. Why does a 7-day rolling mean remove a weekly cycle from daily data?

    A window of exactly one period contains every position in the cycle once, so the repeating pattern cancels in the average.
  2. 2. When should you choose a multiplicative decomposition over an additive one?

    A multiplicative model describes swings proportional to the level; additive describes constant-size swings.
  3. 3. A chart shows December sales 40% above November. What is the first thing to check before calling it growth?

    Seasonality can explain a regular jump. Year-over-year comparison holds the season constant.
  4. 4. Why is a global 3-standard-deviation rule a poor anomaly detector for a trending, seasonal series?

    The rule assumes a constant center. Remove the expected pattern first, then look at deviations from it.
  5. 5. What is a risk of using a centered rolling mean as a feature to predict next week's sales?

    A centered window looks forward in time. For prediction, features must use only information available at that moment.

Summary

  • A time series mixes trend, seasonality, noise and events; EDA separates them.
  • Check for a complete, sorted datetime index first; resample to a scale that fits the question and beware of partial periods.
  • Rolling means smooth noise; a window of one full cycle removes that cycle but lags behind changes.
  • seasonal_decompose estimates trend, seasonal and residual parts (additive or multiplicative).
  • Compare year over year to hold the season fixed, and find anomalies by comparing to an expected local level.

Next: Principles of Effective Visualization, how to turn these patterns into clear charts.