Ir para o conteúdo

Lição 17 · Time and Groups

Exploring Time Series

Explore data over time: check gaps and partial periods, resample, smooth with rolling windows, separate trend from weekly seasonality and compare fairly.

A time series is a set of measurements ordered in time, such as daily sales or hourly temperature. Order carries information: neighbors are often similar, weekly patterns repeat and things change. Methods that shuffle rows or ignore dates miss all of this.

First checks on a timeline

Before you draw trends, check the timeline itself:

  • What are the first and last timestamps?
  • What interval do you expect: hourly, daily, monthly?
  • Are some periods missing or repeated?
  • Is the first or last period partial?
  • Which time zone is used?
Python Find missing and duplicated days

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.

A missing day might mean zero sales, a closed shop or a failed import. These need different treatment. Only fill with zero when you know that nothing happened.

The parts of a series

What can move a series?

Trend Long-term direction
Seasonality Fixed repeating pattern
Events Holidays, launches, outages
Noise Irregular variation
1 / 2

Seasonality repeats with a known period: hours in a day, days in a week, months in a year. Longer ups and downs without a fixed period are often called cycles.

Rolling averages

A rolling average replaces each point with the mean of a window around or before it. With daily data that has a weekly pattern, a 7-day window includes each weekday once, so a stable weekly shape mostly cancels out.

Python A 7-day rolling mean

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.

This is a trailing window: it uses the current day and six earlier ones. It therefore lags behind real changes. A centered window lags less but needs future values, so it cannot be computed in full for the latest days. See rolling windows.

Weekday profiles

To see seasonality directly, group by the repeating unit.

Python Average by weekday

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.

An index above 1 marks days that are above average. This simple profile works best when each weekday appears equally often. A strong trend also adds a little to the later days of each week, so remove it first when it is steep.

Resampling

Resampling changes the time interval, for example from daily to weekly. Choose the right aggregation: sum for flows such as sales, mean or last for levels such as temperature or stock on hand.

Python Weekly totals with completeness

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 last week has only three days. Its total looks like a collapse, but the week is simply unfinished. Mark or remove partial periods before you compare totals. More in resample.

Comparing periods

  • Previous period: this week against last week. Sensitive to seasonality and holidays.
  • Same period last year: accounts for yearly seasons, but check that weekdays and holidays line up.
  • Percent change: (new − old) / old. It is unstable when the old value is small and undefined when it is zero.

Also watch for structural breaks: sudden lasting shifts caused by a new price, a changed tracking method or a new data source. Mark known events on your chart. A jump on the day tracking changed may not be a real change at all.

Two series that both grow over time will usually be correlated, even if they have nothing to do with each other. Compare their changes from one period to the next, or remove trend and seasonality first, before you read a relationship. One series can also follow another with a delay (a lag), so plot both over time.

Do not extend a short trend line far into the future. Exploration describes the past. Forecasting needs its own methods and testing on later data.

For modeling ideas, see time series basics.

A time series checklist for your notes

Write down the time zone, the interval, the first and last complete periods, the missing periods and the known events. Then describe the series in one or two sentences, for example: "Daily sales rise slowly over twelve weeks, with a regular weekend peak and no obvious gaps." Such a description helps you notice when new data stops fitting the pattern.

Be careful with averages over uneven periods. Months have different numbers of days and of weekends, so a monthly total can change even if daily behavior stays the same. Comparing the average per day, or per business day, can be fairer.

Try it yourself

In the rolling-mean example, add a promotion that raises sales by 40 for the days from index 40 to 46. Look at how the 7-day line responds and how long the effect remains visible. Then try a 3-day window and explain why the weekly pattern returns.

Event time and recording time

A record can have more than one timestamp: when something happened, when the system received it and when it was last updated. Group by the one that matches your question. Data that arrives late can make recent days look low at first and higher a week later. If you can, compare two exports of the same period taken on different dates. If the totals changed, tell readers how long it takes before a period is complete enough to report.

A common mistake

Do not choose the start date of a chart because it gives the most dramatic line. Starting at an unusually low week can make ordinary recovery look like strong growth. Show enough history to reveal what normal variation looks like, and explain why the window starts where it does. If your conclusion changes when the window moves by a few weeks, report that too.

Check your understanding

Quick check · 4 questions

of 4 answered You scored / 4 · perfect!

  1. 1. Why does a 7-day window help with daily data that has a weekly pattern?

    Equal weekday coverage evens out the repeating pattern.
  2. 2. The latest week's total is much lower than usual. What should you check first?

    Partial periods often look like sudden drops.
  3. 3. How should you aggregate a daily stock-on-hand level to a month?

    Adding levels over days does not give a meaningful stock quantity.
  4. 4. Two unrelated series both rise for years. What might happen?

    Compare changes or detrended values to reduce this effect.

Summary

  • Check range, interval, gaps, duplicates, partial periods and time zones.
  • Think in terms of trend, seasonality, events and noise.
  • Rolling means smooth data; trailing windows lag.
  • Choose resampling aggregations that match the meaning of the measure.
  • Compare complete, similar periods, and beware of shared trends.

Next: Group Comparisons and Cohorts

Seu progresso fica salvo neste navegador. Não precisa de conta.