Skip to content
Tutorials Center
My learning

Topics

Lesson 33 of 38 ยท Statistical Estimation

Time Series Plots and Rolling Averages with Seaborn

Plot dates with lineplot, format date axes, resample to weekly or monthly values, add a rolling average and compare periods, with pandas doing the smoothing.

Data collected over time is everywhere: sales per day, temperature per hour, visitors per minute. The first plot you want is a line over time. The second is usually the same line smoothed, because daily data is noisy and the trend is what you care about. Seaborn plots the lines and pandas computes the smoothing; this lesson shows how they fit together.

You will plot a series with a proper date axis, aggregate it by week or month, add a rolling average, show the noise and the trend together, and compare several years on one set of axes.

Dates as the x axis

Seaborn understands datetime64 columns: pass them as x and Matplotlib formats the ticks. Make sure your column is a real datetime, not text; convert with pd.to_datetime().

Python A daily time 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.

Seaborn sorts the data by x before drawing (sort=True), so unsorted rows do not zigzag. The jagged shape is mostly noise plus a weekly cycle; the upward trend is hard to see.

Rolling averages with pandas

A rolling (moving) average replaces each point with the mean of a window of nearby points. In pandas: df["visits"].rolling(window=7).mean(). A 7-day window removes a weekly cycle exactly. By default the first window - 1 values are NaN, because there is not enough data; use min_periods=1 to start early, or center=True to align the window around each point.

Python Noise and trend together

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 grey line keeps the raw data visible so you can judge how much smoothing you applied. Bigger windows are smoother but lag more and blur sudden changes.

Alternatives in pandas worth knowing:

Method Idea
rolling(7).mean() Equal weight inside the window
ewm(span=7).mean() Exponential weights: recent points count more
rolling(7).median() Robust to spikes
rolling("7D") Time-based window; works with uneven timestamps
expanding().mean() Mean of everything so far

Resampling: from daily to weekly and monthly

Smoothing keeps one point per day. Resampling changes the frequency: group rows into weeks or months and aggregate. It needs a datetime index (or on="date"):

Python Resample to weeks and months

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.

Frequency codes: "D" day, "W" week (ending Sunday), "MS" month start, "QS" quarter start, "YS" year start, "h" hour. Use .sum() for totals (sales) and .mean() for levels (temperature).

Comparing periods: one line per year

To compare seasons, split the date into parts. Plot the day of year on x and the year as hue, so each year is a line over the same horizontal scale:

Python One line per year

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.

Here many days fall in each month, so lineplot aggregates them: the line is the monthly mean and the band is one standard deviation (see error bars). That is the key idea of lineplot: repeated x values are summarised, not drawn as a zigzag.

Common mistakes

  • Dates stored as strings. Axes show categories, not time. Convert with pd.to_datetime.
  • Rolling window larger than the data you care about. A 90-day average of a 100-day series is nearly flat.
  • Forgetting NaN at the edges. Rolling creates missing values; the line simply starts later. Use min_periods if that is a problem.
  • Averaging an uneven series. If rows are not evenly spaced, use a time-based window (rolling("7D", on="date")) or resample first.
  • Mixing time zones or irregular gaps. Missing days draw straight lines across the gap; reindex with asfreq("D") to make gaps visible.
  • Smoothing and then testing. A smoothed series has strongly dependent points; do not feed it to a test that assumes independence.

For more on date handling and time series, see pandas time series and Matplotlib dates.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. What does df['y'].rolling(7).mean() return for the first row?

    With the default min_periods equal to the window, the first six values are NaN.
  2. 2. What does center=True do in rolling()?

    Without center, a window ends at the current row and the average lags.
  3. 3. What does resample('MS', on='date').sum() do?

    resample groups rows into time bins; MS means month start.
  4. 4. Why keep the raw line grey behind a smoothed line?

    Readers can judge the smoothing against the underlying data.

Summary

  • Use real datetime columns on the x axis; seaborn sorts and Matplotlib formats them.
  • rolling(n).mean() smooths; ewm() weights recent values; resample() changes frequency.
  • Show the raw data lightly beside the smoothed line.
  • Use year as hue and month or day of year on x to compare seasons.

Next: Estimators and Aggregation