Skip to content
Tutorials Center
My learning

Topics

Lesson 1 of 40 ยท Getting Started

What Is a Time Series?

Learn what a time series is, how it differs from ordinary tabular data, the main goals of time series analysis, and how to build and plot your first series with pandas.

A time series is a sequence of measurements taken over time, usually at regular intervals: daily sales, hourly electricity demand, monthly rainfall, a heart rate sampled every second. The one thing that makes it different from an ordinary table is that the order of the rows carries information. If you shuffle a customer table, nothing is lost. If you shuffle a time series, you destroy the trend, the seasons and the memory that tomorrow's value has of today's.

This course teaches you to describe, model and forecast such data with Python. Everything runs in your browser, so you can edit every example. We use pandas for data handling, NumPy for simulation, and statsmodels and scikit-learn for models. All data is simulated with a fixed random seed, so you get the same numbers every time and can compare your changes with the originals.

Why time series are special

Most statistics assumes that observations are independent. Time series break that assumption in three ways:

  • Dependence. Today's temperature is close to yesterday's. Neighbouring values are correlated, so ordinary standard errors and tests can be badly wrong.
  • Structure. A series often has a long-term direction (trend), a repeating pattern (seasonality) and random noise on top.
  • Time-aware evaluation. You can only use the past to predict the future. Random train/test splits leak the future into training, which gives flattering but false scores. The evaluation lessons later show the right way.

Goals of time series analysis

People analyse time series for four main reasons:

Goal Question Example
Description What does the data look like? Is there a trend? Which month is the peak?
Explanation Why did it change? Did a price cut raise sales?
Forecasting What happens next? How much stock to order for next month?
Control and monitoring Is something wrong now? Is this server traffic spike an attack?

Forecasting gets most of the attention, but description comes first: you cannot forecast what you have not looked at.

Types of time series

Univariate or multivariate. One variable over time (daily temperature) versus several recorded together (temperature, humidity, wind). This course starts with univariate series and returns to extra predictors in the machine learning chapters.

Regular or irregular. Regular series have a fixed step: every day, every month. Irregular series, such as event logs or trades, have gaps and random arrival times. Most classical methods expect regular spacing, so irregular data is first resampled (see the datetime index).

Continuous or discrete values. Temperature is continuous. Counts of visitors are discrete and often need special models.

Frequency. The number of observations per natural cycle: 12 for monthly data with a yearly cycle, 7 for daily data with a weekly cycle, 24 for hourly data with a daily cycle. Frequency (also called the seasonal period) comes up in nearly every model.

Not every ordered dataset is a time series. A list of houses sorted by price is just sorted. A time series is ordered by a time stamp that means something physically.

Your first series

A pandas Series with a DatetimeIndex is the standard container. The code below simulates three years of monthly sales with an upward trend, a yearly cycle and noise.

Python Build a monthly 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.

The index is the time axis, the values are the measurements. Notice freq=<MonthBegin>: pandas knows the series is regular, which lets later steps such as shifting and resampling work without guessing.

Look at it first

Always plot before modelling. A single picture reveals trend, seasons, outliers and level shifts that no summary statistic shows.

Python Plot the 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.

You should see a rising line with a wave that repeats every twelve points. Those two features, the rise and the wave, are the first things the next chapter teaches you to separate.

A quick numeric look

Plots are for humans, summaries are for checking. For a series, the most useful ones are the mean and spread per year, and the change from one period to the next.

Python Summaries by 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.

The yearly mean climbs by roughly 36 units a year (3 per month), which is the trend. A plain mean and standard deviation of the whole series would mix trend and season together and describe neither well.

The road ahead

flowchart LR
    A["Getting started: data, pandas, baselines"] --> B["Components and decomposition"]
    B --> C["Stationarity and transformations"]
    C --> D["Smoothing and exponential methods"]
    D --> E["ARIMA and SARIMA"]
    E --> F["Machine learning forecasting"]
    F --> G["Evaluation, anomalies, projects"]

The course moves from looking at data, to classical models, to machine learning, to evaluation and real projects.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. What makes a time series different from an ordinary table of rows?

    Observations are dependent and ordered in time; shuffling them destroys trend, seasonality and autocorrelation.
  2. 2. A daily series with a weekly cycle has which seasonal period?

    The seasonal period is the number of observations in one cycle: seven days in a week.
  3. 3. Why is a random train/test split a poor choice for time series?

    With a random split the model sees values from after the test points, which is impossible in real forecasting and inflates scores.
  4. 4. Which of these is a monitoring goal rather than a forecasting goal?

    Monitoring asks whether the present value is abnormal, not what the future will be.

Summary

  • A time series is a time-ordered sequence of measurements; order is information.
  • Neighbouring values are dependent, so independence-based methods can mislead.
  • Goals are description, explanation, forecasting and monitoring.
  • Series can be univariate or multivariate, regular or irregular; frequency is the number of observations per cycle.
  • A pandas Series with a DatetimeIndex is the standard container; always plot first.

Next: The Datetime Index in pandas