Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Some links on this page are affiliate links: if you buy through them we may earn a commission, at no extra cost to you.

Time-series forecasting is not ordinary machine learning with a date column added. The order of observations matters, the future must remain hidden during validation, and a model is useful only if it beats a sensible baseline at the horizon you actually care about.

This updated guide follows the teaching path of Analytics Vidhya’s “A Guide to Time Series Analysis and Forecasting”, originally published in 2022 and marked last updated on 11 February 2025. It corrects several outdated examples and provides a leakage-safe workflow using current Python libraries.

What is time-series analysis?

Time-series data consists of observations recorded in time order. Examples include daily sales, hourly electricity demand, monthly revenue, website traffic, sensor readings, weather measurements and medical signals.

What’s actually slowing this PC down?

Pick the symptom - the matching free tool is one click away.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Time matters for two reasons:

  • Past observations may influence future observations.
  • Rows cannot usually be shuffled without changing the problem.

The spacing between observations may be regular, such as one reading every hour, or irregular, such as medical events recorded whenever they occur. A model designed for daily data can misinterpret irregular gaps unless the timestamps are handled explicitly.

Time-series analysis examines historical structure, such as trend, seasonality, dependence and unusual events. Forecasting estimates future values. They overlap, but they are not the same activity.

Related tasks include:

  • Nowcasting: estimating the current or very recent value when reporting is delayed.
  • Anomaly detection: identifying observations that do not fit expected temporal behaviour.
  • Causal time-series analysis: estimating the effect of an intervention, policy or external variable.

A useful forecast is not necessarily the most complicated one. It is the one that performs reliably on unseen future data and provides information in a form suitable for the decision being made.

The main components of a time series

A series can contain several overlapping patterns:

  • Level: the typical magnitude of the series.
  • Trend: persistent long-term movement upward or downward.
  • Seasonality: a repeating pattern with a known or reasonably stable period, such as weekday demand or annual retail sales.
  • Cycle: longer-term movement without a fixed, known period, such as economic expansion and contraction.
  • Noise: irregular variation that the model does not explain.
  • Calendar effects: holidays, weekends, month length, fiscal periods and promotions.
  • Structural breaks: abrupt changes caused by a product launch, policy change, disaster, strike or measurement change.

Two standard decomposition forms are:

yt = Tt + St + Rt for additive structure, and:

yt = Tt × St × Rt for multiplicative structure.

Additive decomposition is appropriate when seasonal fluctuations remain roughly the same size. Multiplicative structure is more suitable when seasonal variation grows with the level of the series. These are useful descriptions, not guarantees that a particular forecasting model will perform well.

Free tools Windows power users keep installed

One-click scans. No signup required.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Load and validate the data

Begin by parsing timestamps, sorting records and setting a time index. Do not use the obsolete squeeze=True argument from older pandas examples.

import pandas as pd

df = pd.read_csv("data.csv")
df["timestamp"] = pd.to_datetime(df["timestamp"], errors="coerce")

df = (
    df.dropna(subset=["timestamp"])
      .sort_values("timestamp")
      .set_index("timestamp")
)

Before modelling, check:

  • Duplicate timestamps.
  • Missing timestamps and unexpected gaps.
  • Time-zone consistency.
  • Daylight-saving transitions.
  • Measurement units and target definitions.
  • Whether observations are genuinely regular.

If the business process expects a regular frequency, resample deliberately:

daily = df.resample("D")["sales"].sum()

The aggregation must match the meaning of the data. Sales or traffic may be summed, while temperature may be averaged and an account balance may use the last reported value. Do not automatically fill missing observations with zero. Zero sales, a closed store, a system failure and an unreported value represent different situations.

Also distinguish variables that are known in advance from variables that become available only after the forecast date. A future holiday calendar may be known; tomorrow’s actual promotion response is not.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Explore the series before choosing a model

Start with a plot and basic diagnostics:

import matplotlib.pyplot as plt

y = daily

y.plot(figsize=(12, 4), title="Sales over time")
plt.show()

print(y.describe())
print("Missing values:", y.isna().sum())
print(y.index.to_series().diff().value_counts().head())

Look for changes in level, trend, repeating patterns, outliers, missing periods and variance that grows with the level. Rolling statistics can help reveal changing behaviour:

rolling_mean = y.rolling(7).mean()
rolling_std = y.rolling(7).std()

ax = y.plot(figsize=(12, 4), alpha=0.5, label="Observed")
rolling_mean.plot(ax=ax, label="7-period mean")
rolling_std.plot(ax=ax, label="7-period standard deviation")
ax.legend()
plt.show()

For a daily series, compare averages by weekday. For monthly data, compare months or fiscal periods. Autocorrelation and partial autocorrelation plots can suggest useful lag structure, but they do not prove that a model is correct. Plots and statistical tests form hypotheses; out-of-sample evaluation determines whether those hypotheses help forecasting.

Build baselines before ARIMA or neural networks

A complex model should not be called successful until it beats a relevant simple forecast on an untouched test period.

Naïve forecasting

The naïve forecast repeats the most recent observation:

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

ŷt+h = yt

For a short-horizon random-walk-like series, this can be surprisingly difficult to beat.

Seasonal-naïve forecasting

The seasonal-naïve method repeats the value from the equivalent previous season:

ŷt+h = yt+h-m

Here, m is the seasonal period: 7 for daily weekday seasonality, 12 for monthly annual seasonality or 24 for hourly daily seasonality, provided those patterns are appropriate.

Moving averages

A moving average is useful for smoothing and can serve as a simple benchmark, but smoothing is not automatically a good forecasting strategy. Evaluate it at the same horizon as every other model.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Use chronological splits and walk-forward validation

Never randomly split ordinary time-series rows. Random splitting can put future information in the training set and make validation results unrealistically optimistic.

A final holdout might look like this:

train = y.iloc[:-60]
test = y.iloc[-60:]

Use the training portion for model selection and reserve the final test period for the final comparison. For more reliable selection, use expanding-window or rolling-window backtesting.

from sklearn.model_selection import TimeSeriesSplit

tscv = TimeSeriesSplit(
    n_splits=5,
    test_size=30,
    gap=0
)

TimeSeriesSplit preserves temporal order and supports test_size, train_size and gap.

  • Expanding window: the training set grows after every fold.
  • Rolling window: the training window moves while retaining a fixed size.
  • Gap: a buffer between training and validation, useful when features have delayed effects or overlapping information.

Validation must reproduce deployment. A one-day-ahead forecast should not be evaluated only as a 30-day-ahead forecast. Recursive forecasts, direct multi-horizon forecasts and multi-output forecasts should each be evaluated in the way they will actually be used.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Stationarity and transformations

A weakly stationary process has statistical properties that remain stable over time. In practical terms, this commonly means a stable mean, stable variance and autocovariance that depends on lag rather than the absolute date.

“No trend or seasonality” is a useful beginner’s approximation, but it is not a complete definition. Stationarity is also not mandatory for every forecasting method. Exponential smoothing, tree-based models and several modern approaches can handle trend or seasonality directly.

Differencing can remove some persistent movement:

y_diff = y.diff().dropna()

For non-negative data with changing variance, a log-style transformation may help:

import numpy as np

y_log_diff = (
    y.clip(lower=0)
     .pipe(lambda s: np.log1p(s))
     .diff()
     .dropna()
)

Differencing changes the target scale and requires an inverse transformation before interpreting forecasts. Log transformations are unsuitable for negative values unless a carefully justified shift is used. Seasonal differencing may be needed when a periodic pattern remains.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

ADF and KPSS tests can provide diagnostic evidence, but they should not be treated as automatic model-selection authorities. A stationary series is not necessarily easy to forecast, and a nonstationary series is not automatically unusable.

Scaling does not make a series stationary

This distinction is important. MinMaxScaler changes the numeric range; it does not remove trend, seasonality, autocorrelation, structural breaks or changing variance.

from sklearn.preprocessing import MinMaxScaler

scaler = MinMaxScaler()
train_scaled = scaler.fit_transform(train.to_numpy().reshape(-1, 1))
test_scaled = scaler.transform(test.to_numpy().reshape(-1, 1))

Fit the scaler only on the training data. Fitting it on the complete dataset leaks information from the future. Scaling is often useful for neural networks and some machine-learning algorithms, but it may be unnecessary for many tree-based and statistical models.

Statistical forecasting models

Exponential smoothing

Simple exponential smoothing models a level. Holt’s method adds a trend, while Holt-Winters adds seasonality. A damped trend can prevent a long-range forecast from increasing indefinitely. These methods are fast, interpretable and strong candidates for a first serious model.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

AR, MA and ARMA

An autoregressive model uses lagged target values. A moving-average model uses lagged forecast errors; it is not simply a rolling average of raw observations. ARMA combines both components for a stationary series.

ARIMA

ARIMA uses:

  • p: autoregressive order.
  • d: differencing order.
  • q: moving-average order.

Use the current statsmodels interface rather than the obsolete statsmodels.tsa.arima_model.ARIMA import:

from statsmodels.tsa.arima.model import ARIMA

model = ARIMA(train, order=(1, 1, 1))
results = model.fit()
forecast = results.get_forecast(steps=len(test))
pred = forecast.predicted_mean
interval = forecast.conf_int()

SARIMA and SARIMAX

SARIMA adds seasonal autoregressive, differencing and moving-average terms. SARIMAX also accepts exogenous variables, such as a known holiday indicator or a planned price.

from statsmodels.tsa.statespace.sarimax import SARIMAX

model = SARIMAX(
    train,
    order=(1, 1, 1),
    seasonal_order=(1, 0, 1, 12),
    exog=train_exog,
    enforce_stationarity=False,
    enforce_invertibility=False,
)

results = model.fit(disp=False)
forecast = results.get_forecast(
    steps=len(test),
    exog=test_exog
)

pred = forecast.predicted_mean
interval = forecast.conf_int()

See the SARIMAX documentation for the supported autoregressive, differencing, seasonal, trend and exogenous-regressor components. For non-seasonal models, consult the current ARIMA documentation.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Do not conclude that ARIMA is automatically better than ARMA because one training fit has a lower residual sum of squares. Compare models using out-of-sample, horizon-appropriate metrics. Inspect residual autocorrelation, changing variance and unusual errors as well.

Feature-based machine learning

Machine-learning models can learn nonlinear effects from lag, rolling and calendar features. A safe feature function shifts rolling calculations so that the value being predicted is not included:

def make_features(frame, target="sales"):
    out = frame.copy()
    out["lag_1"] = out[target].shift(1)
    out["lag_7"] = out[target].shift(7)
    out["rolling_7"] = out[target].shift(1).rolling(7).mean()
    out["dayofweek"] = out.index.dayofweek
    out["month"] = out.index.month
    return out.dropna()

Possible models include linear regression, Ridge, Elastic Net, random forests and gradient boosting. XGBoost and LightGBM are additional options subject to licensing and deployment constraints.

Common leakage sources include unshifted rolling features, fitting encoders or scalers on all dates, using revised future values and tuning on the final test set. If future external variables are required, their future values must genuinely be available when the forecast is produced.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Where Prophet and deep learning fit

Prophet can be useful when a series has interpretable trend, holidays and seasonality, and a quick additive model is desired. Its standard input uses columns named ds for the timestamp and y for the target. It is not universally superior and is not automatically the right choice for intermittent demand, hierarchical forecasting, arbitrary high-frequency data or causal analysis.

RNNs, LSTMs, temporal convolutional networks and transformer-style models can represent complex sequential relationships. However, they require careful window construction, scaling, leakage-safe validation, tuning, monitoring and usually more data and engineering than simpler models.

The official TensorFlow time-series tutorial demonstrates windowing, forecasting and sequence models. A neural network should normally come after naïve, seasonal-naïve, exponential-smoothing and suitable statistical or feature-based models. A visually close prediction line is not evidence of generalisation.

Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

Evaluate forecasts with the right metrics

  • MAE: average absolute error in the target’s units.
  • RMSE: penalises large errors more heavily than MAE.
  • MAPE: unstable or undefined when actual values are zero or near zero.
  • sMAPE: not universally stable despite its name.
  • WAPE: useful for aggregate demand, but can hide failures in smaller segments.
  • MASE: compares performance with a naïve benchmark.
  • Pinball loss: evaluates quantile forecasts.
  • Interval coverage: checks whether prediction intervals contain the actual values at the expected rate.

Always report the forecast horizon, evaluation window, aggregation level, transformation scale and baseline performance. For business use, examine errors by product, location, customer segment and season rather than relying only on one overall average.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

A point forecast is incomplete for decisions involving inventory, staffing, capacity or financial risk. Statistical models can provide prediction intervals, while quantile models and conformal methods offer other routes to uncertainty estimates. Intervals must also be checked for calibration.

Important edge cases

Multiple seasonalities

Hourly data may contain daily, weekly and annual patterns simultaneously. A basic seasonal ARIMA model may not handle all of them conveniently. Calendar features, dynamic harmonic regression and specialised multi-seasonal methods may be more suitable.

Intermittent demand and many zeros

Ordinary ARIMA can perform poorly when demand is mostly zero with occasional transactions. Consider Croston-style or TSB methods, or a classification-plus-regression strategy. MAPE is especially misleading in this setting.

Count data

Visits, incidents and transactions may be counts rather than continuous Gaussian measurements. Poisson or negative-binomial approaches may better reflect their distribution.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Outliers and interventions

Do not delete every unusual value. An outlier may represent a promotion, supply shortage, weather event, measurement error or permanent change. Keep a record of intervention dates and model known events where appropriate.

Structural breaks

A model trained on pre-pandemic behaviour may fail after a policy, product or market change. Consider event indicators, rolling training windows, retraining rules and drift monitoring.

Recursive error accumulation

Autoregressive models that feed their predictions back as future inputs can accumulate error over long horizons. Compare recursive, direct and multi-output strategies using the actual deployment horizon.

Production considerations

A notebook result is not a forecasting system. Production planning should define:

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
  • How often data is refreshed and forecasts are generated.
  • When the model is retrained.
  • Which data version and code version produced each forecast.
  • How missing inputs and late-arriving data are handled.
  • How forecast errors, drift and interval coverage are monitored.
  • When a fallback naïve forecast is used.
  • How a bad model is rolled back.

For most learners, Python with pandas, scikit-learn and statsmodels is enough. Install a reproducible local environment with:

python -m venv .venv
source .venv/bin/activate        # macOS/Linux
# .venvScriptsactivate         # Windows PowerShell

python -m pip install --upgrade pip
pip install pandas numpy matplotlib scikit-learn statsmodels

Install Prophet or TensorFlow only when the problem justifies them, and record the package versions used:

pip freeze > requirements.txt

Managed services such as Amazon SageMaker, Azure Machine Learning and Google Vertex AI become relevant when an organisation needs managed training, deployment, governance, identity controls or large-scale retraining. Their costs depend on region, compute, storage, runtime and related services, so they are not necessary for a small local ARIMA experiment.

What should not be copied unchanged from the older guide?

The Analytics Vidhya article remains a useful introductory overview, but several parts need updating for current projects:

What’s actually slowing this PC down?

Pick the symptom - the matching free tool is one click away.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
  • Use statsmodels, not the misspelled statmodels.
  • Use modern statsmodels.tsa.arima.model.ARIMA or SARIMAX imports.
  • Do not rely on the old pandas squeeze=True pattern.
  • Do not describe scaling as a way to remove trend or seasonality.
  • Do not use a simple 80/20 split as the only validation method.
  • Do not select a model from visual plots or training residual sums of squares alone.
  • Do not treat an LSTM or ARIMA result on one example as proof of general superiority.
  • Include forecast intervals, not only point predictions.

A practical model-selection path

  1. Confirm the timestamp, frequency, target and data availability rules.
  2. Plot the series and investigate gaps, outliers, seasonality and structural breaks.
  3. Build naïve and seasonal-naïve forecasts.
  4. Choose a chronological holdout and walk-forward validation design.
  5. Try exponential smoothing for trend and seasonality.
  6. Try ARIMA or SARIMAX for structured univariate data and useful external regressors.
  7. Add lag, rolling and calendar features for machine-learning models.
  8. Consider Prophet when additive trend, holidays and seasonality are the main requirements.
  9. Use deep learning only when data volume and problem complexity justify its additional cost and operational risk.
  10. Compare every candidate with baselines on the original target scale and at the real forecast horizon.

The best model may be a naïve forecast, an exponential-smoothing model, a regression, a boosted-tree model or a neural network. Model choice should follow the data, deployment constraints and evaluation results—not the popularity of the algorithm.

Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.