IntermediateLesson 14 of 306 min read

Dates and Time Series: Months, Partial Periods and Seasons

Resample sessions into months, avoid the unfinished-month trap, measure seasonality with an index, and smooth with a rolling average.

In this lesson you will

  • Resample records into months
  • Use rates when periods differ
  • Spot and handle an unfinished period
  • Measure seasonality and smooth a series

About 28 minutes

A year in the life of the workshop

By the autumn, Leila has been counting visits since January, and she wants to plan volunteers for next year. The 40-row file from the first course covers only two months, which is far too short to see a yearly pattern. For this lesson, imagine a longer record: the number of visits at each session from January to the end of October. It is made up, like everything else here, and a small program creates it with a fixed seed so that you get the same numbers every time. The workshop opens on Tuesdays and Saturdays.

Dates are special

A date column is more than a label. It has an order, and it has a calendar: months of different lengths, weeks that start on different days, and sessions that fall on some days and not others. Tools that understand dates can resample, which means regrouping rows by week or month without you writing the calendar rules. They can also slide a rolling window along the series to smooth out the wobble. The program uses both.

import random, pandas as pd
r = random.Random(14)
base = {1: 2, 2: 2, 3: 3, 4: 4, 5: 5, 6: 5, 7: 4, 8: 3, 9: 5, 10: 5}
days = pd.date_range("2026-01-03", "2026-10-24", freq="D")
sessions = [d for d in days if d.weekday() in (1, 5)]
counts = [max(0, base[d.month] + r.choice([-1, 0, 0, 1, 2])) for d in sessions]
ts = pd.Series(counts, index=pd.DatetimeIndex(sessions), name="visits")
ts.rename_axis("date").reset_index().to_csv("sessions.csv", index=False)
print("sessions:", len(ts), "| visits:", int(ts.sum()))
month = ts.resample("ME").agg(["sum", "count"])
for day, row in month.iterrows():
    print(day.strftime("%b") + ": " + str(int(row["sum"])) + " visits, " + str(int(row["count"])) + " sessions, " + str(round(row["sum"] / row["count"], 1)) + " each")
last = ts.index.max()
print("last session:", last.strftime("%d %b"))
print("October is partial, its total of", int(month["sum"].iloc[-1]), "is not comparable with September")
full = month.iloc[:-1]
per = full["sum"] / full["count"]
index = (per / per.mean()).round(2)
print("busiest month:", index.idxmax().strftime("%b"), "index", index.max())
print("quietest month:", index.idxmin().strftime("%b"), "index", index.min())
print("last 4 sessions average:", round(ts.rolling(4).mean().iloc[-1], 2))
sessions: 85 | visits: 354
Jan: 21 visits, 9 sessions, 2.3 each
Feb: 18 visits, 8 sessions, 2.2 each
Mar: 31 visits, 9 sessions, 3.4 each
Apr: 29 visits, 8 sessions, 3.6 each
May: 52 visits, 9 sessions, 5.8 each
Jun: 50 visits, 9 sessions, 5.6 each
Jul: 35 visits, 8 sessions, 4.4 each
Aug: 26 visits, 9 sessions, 2.9 each
Sep: 52 visits, 9 sessions, 5.8 each
Oct: 40 visits, 7 sessions, 5.7 each
last session: 24 Oct
October is partial, its total of 40 is not comparable with September
busiest month: May index 1.44
quietest month: Feb index 0.56
last 4 sessions average: 5.5

The record has 85 sessions and 354 visits. The monthly lines show two numbers that are easy to confuse: the total visits and the number of sessions. They differ because months have four or five of each weekday. Compare March and April: the totals are 31 and 29, but the visits per session are 3.4 and 3.6. When months have different numbers of sessions, per-session figures are the fair comparison.

The partial period trap

The last session in the record is 24 Oct. October is therefore unfinished: its total of 40 visits comes from 7 sessions only. If you compared that total with September’s 52, October would seem to have dropped sharply, when in fact the per-session average is almost the same. This trap appeared in the first course for partial weeks, and it is the most common mistake in reports that compare periods. The fix is to compare like with like: complete months with complete months, or rates per session with rates per session. A good report marks any unfinished period clearly.

Seasonality

Look at the line of monthly averages. The quiet months are the cold ones at the start of the year and, in this made-up workshop, August, when many volunteers and customers are away. The busy months are spring and the early autumn. A repeating pattern tied to the calendar is called seasonality. To measure it, the program divides each full month’s per-session average by the overall average, giving a seasonal index. An index of 1.00 means an average month. The busiest month is May at 1.44, and the quietest is Feb at 0.56. In words, February is about half as busy as an average month, and May is more than a third busier.

Seasonality changes what counts as a surprise. A drop in February is normal. The same drop in May would deserve attention. Without a seasonal view, you would raise an alarm every winter and celebrate every spring.

Smoothing with a rolling average

A single session is noisy: five visits one day, two the next. A rolling average of the last four sessions, 5.5 visits at the end of the record, shows the level more calmly. The price of smoothing is delay, because the average reacts slowly to a real change, and it also hides short spikes. Choose a window that matches the decision: four sessions is two weeks here, which suits planning volunteers, but is too slow for spotting a one-day problem.

Rules for time series

  • Mark unfinished periods and leave them out of comparisons, or use rates.
  • Compare the same season when you can: this March with last March, not March with August.
  • Use rates when periods differ in length, such as visits per session or per day open.
  • Check the calendar. Holidays, closures and extra sessions change counts without any change in demand.
  • Do not draw a straight line through a seasonal pattern. A trend line over eight months of a yearly cycle mostly measures where in the cycle you started.
Common mistake: partial periods

Comparing a total from an unfinished month with a total from a full month. The unfinished month always looks worse. Compare rates, or wait until the month ends.

One year is not enough to be sure

The visit counts for this lesson are made up with a seed, and the seasons are built in on purpose. A real workshop would have its own pattern. With only one year you cannot be sure a pattern repeats.

Take any count you track by month. For each month, write down how many days or sessions it had, then work out a rate per day. See whether the ranking of months changes.

Practice for this lesson

Check what you learned

Five quick questions. Pick an answer and you will see straight away whether it is right and why.

What does resampling do?

  1. It deletes old rows
  2. It sorts text
  3. It regroups rows by a calendar period such as month
  4. It copies the file

The monthly lines were made by resampling the sessions.

Why compare visits per session across months?

  1. Sessions are always equal
  2. It is faster to type
  3. Totals are illegal
  4. Months have different numbers of sessions

Per-session rates are the fair comparison when months differ.

Why is the latest month a trap?

  1. It always has more visits
  2. It is unfinished, so its total looks too low
  3. It has no dates
  4. It is spelled differently

October had only seven sessions so far.

What does a seasonal index of 1.00 mean?

  1. The busiest month
  2. No visits at all
  3. An average month
  4. A broken record

Above 1.00 is busier than average, below is quieter.

What is the cost of a rolling average?

  1. It reacts slowly to a real change
  2. It adds more rows
  3. It removes the dates
  4. It changes the totals

Smoothing calms noise but delays the signal.

Summary

  • Resample by calendar period and compare rates, not raw totals.
  • An unfinished period always looks low.
  • A seasonal index shows how a month compares with average.
  • A rolling average calms noise but lags real changes.

Next lesson

Lesson 15 asks how sure we can be: samples, standard errors and the bootstrap.

Questions

What is the difference between resampling and grouping?

Resampling is grouping by calendar periods for data indexed by date. It knows about months and weeks, so you do not write the calendar rules yourself.

How many years do I need to measure seasonality?

More is better. With one year you see a pattern but cannot tell whether it repeats or whether this year was unusual. Several years give much firmer evidence.

Should I remove the seasonal pattern before analysing?

Often you compare with the same season instead, or divide by the seasonal index. The right choice depends on the question you are answering.

Progress is saved in this browser.