More about forecasting in cienciadedatos.net


No description has been provided for this image

Introduction

Although historical data is available in many real use cases of forecasting, not all of it is reliable. Some examples of these scenarios are:

  • IoT sensors: within the Internet of Things, sensors capture the raw data from the physical world. Often the sensors are deployed or installed in harsh environments. This inevitably means that the sensors are prone to failure, malfunction, and rapid attrition, causing the sensor to produce unusual and erroneous readings.

  • Factory shutdown: factories are periodically shut down for repair, overhaul, or maintenance activities. These events cause production to stop, generating a gap in the data.

  • Pandemic (COVID-19): the COVID-19 pandemic changed population behavior significantly, directly impacting many time series such as production, sales, and transportation.

The presence of unreliable or unrepresentative values in the data history can be a problem, since the model may learn patterns that will not be repeated in the future. For most forecasting algorithms, removing that part of the data is not an option because they require the time series to be complete. An alternative solution is to reduce the weight of the affected observations during model training. This document shows two examples of how skforecast makes it easy to apply this strategy, and how to check whether it actually improves the predictions.

✎ Note

In the following examples, a portion of the time series is excluded from model training by giving it a weight of zero. However, the use of weights is not limited to including or excluding observations: weights can also be used to balance the degree of influence of each observation on the forecasting model. For example, an observation with a weight of 10 has 10 times more impact on the model training than an observation with a weight of 1.

⚠ Warning

In most gradient boosting implementations (LightGBM, XGBoost, CatBoost), samples with zero weight are ignored when calculating the gradients and hessians. However, the values for those samples are still considered when building the feature histograms. Therefore, the resulting model may differ from the model trained without the zero-weighted samples. See more details in this issue.

For linear models such as the Ridge regressor used in this document, a weight of zero is equivalent to removing the observation from the training set.

Libraries

# Data processing
# ==============================================================================
import pandas as pd
import numpy as np
from skforecast.datasets import fetch_dataset

# Plots
# ==============================================================================
import matplotlib.pyplot as plt
from skforecast.plot import set_dark_theme

# Modelling and Forecasting
# ==============================================================================
import skforecast
import sklearn
from sklearn.linear_model import Ridge
from skforecast.recursive import ForecasterRecursive
from skforecast.model_selection import TimeSeriesFold
from skforecast.model_selection import backtesting_forecaster

# Configuration
# ==============================================================================
import warnings
warnings.filterwarnings('once')
set_dark_theme()

color = '\033[1m\033[38;5;208m'
print(f"{color}Version skforecast: {skforecast.__version__}")
print(f"{color}Version scikit-learn: {sklearn.__version__}")
print(f"{color}Version pandas: {pd.__version__}")
print(f"{color}Version numpy: {np.__version__}")
Version skforecast: 0.27.0
Version scikit-learn: 1.9.1
Version pandas: 2.3.3
Version numpy: 2.5.4

COVID-19 lockdown

During the lockdown period imposed as a consequence of the COVID-19 pandemic, the behavior of the population was altered. An example of this can be seen in the use of the bicycle rental service in the city of Madrid (Spain).

Data

# Data download
# ==============================================================================
data = fetch_dataset('bicimad')
data.head()
╭──────────────────────────────────────────────────── bicimad ────────────────────────────────────────────────────╮
│ Description:                                                                                                    │
│ This dataset contains the daily users of the bicycle rental service (BiciMad) in the city of Madrid (Spain)     │
│ from 2014-06-23 to 2022-09-30.                                                                                  │
│                                                                                                                 │
│ Source:                                                                                                         │
│ The original data was obtained from: Portal de datos abiertos del Ayuntamiento de Madrid                        │
│ https://datos.madrid.es/portal/site/egob                                                                        │
│                                                                                                                 │
│ URL:                                                                                                            │
│ https://raw.githubusercontent.com/skforecast/skforecast-datasets/main/data/bicimad_users.csv                    │
│                                                                                                                 │
│ Shape: 3022 rows x 1 columns                                                                                    │
╰─────────────────────────────────────────────────────────────────────────────────────────────────────────────────╯
users
date
2014-06-23 99
2014-06-24 72
2014-06-25 119
2014-06-26 135
2014-06-27 149

The analysis is restricted to the years 2020 and 2021. The series is split chronologically: the data up to 2021-06-01 is used to train the model (518 days) and the remaining 213 days are used as the test set. In the plot, the shaded areas mark the lockdown (2020-03-16 to 2020-04-21) and the subsequent recovery period (until 2020-05-31).

The training set contains a second, shorter disruption that is not related to the pandemic: in January 2021, a heavy snowstorm (storm Filomena) brought the city to a standstill. The number of users dropped to almost zero and did not return to its usual values until the end of the month (2021-01-08 to 2021-01-31). This period is also highlighted in the plot.

# Split data into train-test
# ==============================================================================
data = data.loc['2020-01-01': '2021-12-31']
end_train = '2021-06-01 23:59:00'
data_train = data.loc[: end_train, :]
data_test  = data.loc[end_train:, :]

print(
    f'Dates train : {data_train.index.min()} --- {data_train.index.max()}  '
    f'(n={len(data_train)})'
)
print(
    f'Dates test  : {data_test.index.min()} --- {data_test.index.max()}  '
    f'(n={len(data_test)})'
)
Dates train : 2020-01-01 00:00:00 --- 2021-06-01 00:00:00  (n=518)
Dates test  : 2021-06-02 00:00:00 --- 2021-12-31 00:00:00  (n=213)
# Time series plot
# ==============================================================================
fig, ax = plt.subplots(figsize=(8, 3))
data_train.users.plot(ax=ax, label='train', linewidth=1)
data_test.users.plot(ax=ax, label='test', linewidth=1)
ax.axvspan(
    pd.to_datetime('2020-03-16'),
    pd.to_datetime('2020-04-21'),
    label='COVID-19 lockdown',
    color='red',
    alpha=0.3
)

ax.axvspan(
    pd.to_datetime('2020-04-21'),
    pd.to_datetime('2020-05-31'),
    label='Recovery time',
    color='white',
    alpha=0.3
)

ax.axvspan(
    pd.to_datetime('2021-01-08'),
    pd.to_datetime('2021-01-31'),
    label='Snowstorm',
    color='yellow',
    alpha=0.3
)

ax.set_title('Number of BiciMAD users')
ax.legend(loc='upper left', bbox_to_anchor=(1.01, 1));
No description has been provided for this image

Include the whole time series

First, a ForecasterRecursive is created with a Ridge regressor and the last 21 days as lags (predictors). All observations have the same weight during training, including those of the lockdown and the snowstorm. This model is used as the baseline.

# Create recursive multi-step forecaster (ForecasterRecursive)
# ==============================================================================
forecaster = ForecasterRecursive(
                 estimator = Ridge(random_state=123),
                 lags      = 21,
             )
forecaster

ForecasterRecursive

General Information
  • Estimator: Ridge
  • Lags: [ 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21]
  • Window features: None
  • Calendar features: None
  • Window size: 21
  • Series name: None
  • Exogenous included: False
  • Categorical features: auto
  • Weight function included: False
  • Differentiation order: None
  • Drop NaN from series: False
  • Creation date: 2026-11-11 21:44:15
  • Last fit date: None
  • Skforecast version: 0.27.0
  • Python version: 3.13.18
  • Forecaster id: None
Exogenous Variables

None

Data Transformations
  • Transformer for y: None
  • Transformer for exog: None
Training Information
  • Training range: Not fitted
  • Training index type: Not fitted
  • Training index frequency: Not fitted
Estimator Parameters
    {'alpha': 1.0, 'copy_X': True, 'fit_intercept': True, 'max_iter': None, 'positive': False, 'random_state': 123, 'solver': 'auto', 'tol': 0.0001}
Fit Kwargs
    {}

📖 API Reference    📝 User Guide

Once the model is created, a backtesting process is run with backtesting_forecaster and TimeSeriesFold to simulate the behavior of the forecaster if it had predicted the test set in 7-day batches. With refit=False, the model is trained only once, using the training set, and then predicts the test set in 31 consecutive folds. The mean absolute error (MAE) is used as the metric.

# Backtesting: predict next 7 days at a time.
# ==============================================================================
cv = TimeSeriesFold(
         steps              = 7,
         initial_train_size = len(data_train),
         refit              = False
     )
metric_baseline, predictions_baseline = backtesting_forecaster(
                                            forecaster = forecaster,
                                            y          = data.users,
                                            cv         = cv,
                                            metric     = 'mean_absolute_error',
                                            verbose    = False
                                        )
metric_baseline
mean_absolute_error n_evaluated
0 1469.144278 213

Exclude part of the time series

To minimize the influence of the lockdown and the snowstorm on the model, a custom function is created and passed to the forecaster through the weight_func argument. Each time the forecaster is fitted, this function receives the index of the training matrix (the date of the target value of each row) and must return one weight per row. The function follows these rules:

  • Weight of 0 if the index date is:

    • Within the lockdown period (2020-03-16 to 2020-04-21).

    • Within the recovery period (2020-04-22 to 2020-05-31).

    • Within the 21 days after the recovery period (2020-06-01 to 2020-06-21). The forecaster uses lags=21, so the rows of the training matrix whose target falls in these dates still use impacted values as predictors.

    • Within the snowstorm period (2021-01-08 to 2021-01-31) or the 21 days after it (2021-02-01 to 2021-02-21), for the same reason.

  • Weight of 1 otherwise.

An observation with a weight of 0 has no influence on the training of the Ridge model (see the warning in the introduction regarding gradient boosting models).

✎ Note

The weights returned by weight_func must be non-negative, must not contain NaN values, and cannot all be zero. If any of these conditions is not met, a ValueError is raised when the forecaster is fitted. The weights of a training set can be inspected with the create_sample_weights method.

⚠ Warning

If the estimator does not accept sample_weight in its fit method, the weight_func argument is ignored and an IgnoredArgumentWarning is raised when the forecaster is created. The model is then trained as if no weights had been provided.

✎ Note

Weights are only used when the model is trained. The observations with a weight of 0 are still part of the time series, so they are used as lags whenever they fall within the last window of data needed to create the predictors. Therefore, weighting does not help if the anomalous period is located right before the dates to be predicted.

# Custom function to create weights
# ==============================================================================
def custom_weights_bicimad(index):
    """
    Return a weight of 0 for the dates affected by the lockdown (2020-03-16 to
    2020-06-21) or by the snowstorm (2021-01-08 to 2021-02-21) and 1 otherwise.
    Both ranges include the 21 days affected through the lags.
    """
    lockdown  = (index >= '2020-03-16') & (index <= '2020-06-21')
    snowstorm = (index >= '2021-01-08') & (index <= '2021-02-21')
    weights = np.where(lockdown | snowstorm, 0, 1)
    return weights

Again, a ForecasterRecursive is created, this time passing custom_weights_bicimad to the weight_func argument. The function is called automatically every time the forecaster is fitted: only once in this backtesting, since refit=False, and on every refit if refit=True.

# Create recursive multi-step forecaster (ForecasterRecursive)
# ==============================================================================
forecaster = ForecasterRecursive(
                 estimator   = Ridge(random_state=123),
                 lags        = 21,
                 weight_func = custom_weights_bicimad
             )

The source code of the function is stored in the source_code_weight_func attribute of the forecaster. This makes it possible to check how the weights were calculated, for example after loading a forecaster saved in a previous session.

# Source code of the weight function stored in the forecaster
# ==============================================================================
print(forecaster.source_code_weight_func)
def custom_weights_bicimad(index):
    """
    Return a weight of 0 for the dates affected by the lockdown (2020-03-16 to
    2020-06-21) or by the snowstorm (2021-01-08 to 2021-02-21) and 1 otherwise.
    Both ranges include the 21 days affected through the lags.
    """
    lockdown  = (index >= '2020-03-16') & (index <= '2020-06-21')
    snowstorm = (index >= '2021-01-08') & (index <= '2021-02-21')
    weights = np.where(lockdown | snowstorm, 0, 1)
    return weights

Before running the backtesting, the weights assigned to each observation are inspected with the create_sample_weights method. It takes the training matrix created with create_train_X_y, which has 497 rows: the 518 training observations minus the first 21, which are only used as lags. Of these rows, 143 have a weight of 0 (98 for the lockdown and 45 for the snowstorm) and 354 a weight of 1.

# Weights assigned to each row of the training matrix
# ==============================================================================
X_train, y_train = forecaster.create_train_X_y(y=data_train.users)
sample_weights = pd.Series(
                     forecaster.create_sample_weights(X_train=X_train),
                     index = X_train.index,
                     name  = 'weight'
                 )
print(sample_weights.value_counts())

fig, ax = plt.subplots(figsize=(8, 2))
sample_weights.plot(ax=ax, linewidth=1)
ax.set_title('Weights of the training observations');
weight
1    354
0    143
Name: count, dtype: int64
No description has been provided for this image
# Backtesting: predict next 7 days at a time.
# ==============================================================================
metric_weighted, predictions_weighted = backtesting_forecaster(
                                            forecaster = forecaster,
                                            y          = data.users,
                                            cv         = cv,
                                            metric     = 'mean_absolute_error',
                                            verbose    = False
                                        )
metric_weighted
mean_absolute_error n_evaluated
0 1399.678037 213
# Comparison of backtesting metrics
# ==============================================================================
mae_baseline = metric_baseline.loc[0, 'mean_absolute_error']
mae_weighted = metric_weighted.loc[0, 'mean_absolute_error']
results = pd.DataFrame(
              {'mean_absolute_error': [mae_baseline, mae_weighted]},
              index = ['Without weights', 'With weights']
          )
results['improvement (%)'] = 100 * (1 - results['mean_absolute_error'] / mae_baseline)
results
mean_absolute_error improvement (%)
Without weights 1469.144278 0.000000
With weights 1399.678037 4.728347
# Backtesting predictions of both models
# ==============================================================================
fig, ax = plt.subplots(figsize=(8, 3))
data_test.users.plot(ax=ax, label='test', linewidth=1)
predictions_baseline.pred.plot(ax=ax, label='without weights', linewidth=1)
predictions_weighted.pred.plot(ax=ax, label='with weights', linewidth=1)
ax.set_title('Number of BiciMAD users: backtesting predictions')
ax.legend();
No description has been provided for this image

Giving a weight of 0 to the lockdown, the recovery period, the snowstorm and the 21 days that follow them (excluding them from the model training) reduces the mean absolute error from 1469 to 1400 users (4.7%).

Power plant shutdown

Power plants are complex installations that require a high level of maintenance. Periodically, a plant has to be shut down for repair, overhaul, or maintenance activities.

Data

# Data download
# ==============================================================================
url = ('https://raw.githubusercontent.com/skforecast/skforecast-datasets/refs/'
       'heads/main/data/energy_production_shutdown.csv')
data = pd.read_csv(url)

# Data preprocessing
# ==============================================================================
data['date'] = pd.to_datetime(data['date'], format='%Y-%m-%d')
data = data.set_index('date')
data = data.sort_index()
data = data.asfreq('D')
data.head()
production
date
2012-01-01 375.1
2012-01-02 474.5
2012-01-03 573.9
2012-01-04 539.5
2012-01-05 445.4

The dataset used in this example contains the daily energy production of a plant. The data from 2012 and 2013 is used to train the model (731 days) and the data from 2014 as the test set (364 days). The plant was shut down between 2012-06-01 and 2012-09-30, a period highlighted in the plot.

# Split data into train-test
# ==============================================================================
data = data.loc['2012-01-01': '2014-12-30']
end_train = '2013-12-31 23:59:00'
data_train = data.loc[: end_train, :]
data_test  = data.loc[end_train:, :]

print(
    f'Dates train : {data_train.index.min()} --- {data_train.index.max()}  '
    f'(n={len(data_train)})'
)
print(
    f'Dates test  : {data_test.index.min()} --- {data_test.index.max()}  '
    f'(n={len(data_test)})'
)
Dates train : 2012-01-01 00:00:00 --- 2013-12-31 00:00:00  (n=731)
Dates test  : 2014-01-01 00:00:00 --- 2014-12-30 00:00:00  (n=364)
# Time series plot
# ==============================================================================
fig, ax = plt.subplots(figsize=(8, 3))
data_train.production.plot(ax=ax, label='train', linewidth=1)
data_test.production.plot(ax=ax, label='test', linewidth=1)
ax.axvspan(
    pd.to_datetime('2012-06-01'),
    pd.to_datetime('2012-09-30'),
    label='Shutdown',
    color='white',
    alpha=0.2
)
ax.set_title('Energy production')
ax.legend();
No description has been provided for this image

Include the whole time series

As before, a baseline ForecasterRecursive is created first, giving the same weight to all observations, including those of the shutdown period. Its performance is evaluated with backtesting, this time predicting the test set in 10-day batches.

# Create recursive multi-step forecaster (ForecasterRecursive)
# ==============================================================================
forecaster = ForecasterRecursive(
                 estimator = Ridge(random_state=123),
                 lags      = 21,
             )
# Backtesting: predict next 10 days at a time.
# ==============================================================================
cv = TimeSeriesFold(
         steps              = 10,
         initial_train_size = len(data_train),
         refit              = False
     )
metric_baseline, predictions_baseline = backtesting_forecaster(
                                            forecaster = forecaster,
                                            y          = data.production,
                                            cv         = cv,
                                            metric     = 'mean_absolute_error',
                                            verbose    = False
                                        )
metric_baseline
mean_absolute_error n_evaluated
0 28.424722 364

Exclude part of the time series

The power plant shutdown took place from 2012-06-01 to 2012-09-30. As in the previous example, a custom function (custom_weights_shutdown) assigns a weight of 0 if the index date is within the shutdown period or up to 21 days later, and 1 otherwise. The extra 21 days are needed because the rows of the training matrix whose target is dated between 2012-10-01 and 2012-10-21 still use values from the shutdown as lags.

# Custom function to create weights
# ==============================================================================
def custom_weights_shutdown(index):
    """
    Return a weight of 0 for dates between 2012-06-01 and 2012-10-21 (shutdown
    plus the 21 days affected through the lags) and 1 otherwise.
    """
    weights = np.where((index >= '2012-06-01') & (index <= '2012-10-21'), 0, 1)
    return weights
# Create recursive multi-step forecaster (ForecasterRecursive)
# ==============================================================================
forecaster = ForecasterRecursive(
                 estimator   = Ridge(random_state=123),
                 lags        = 21,
                 weight_func = custom_weights_shutdown
             )

# Backtesting: predict next 10 days at a time.
# ==============================================================================
metric_weighted, predictions_weighted = backtesting_forecaster(
                                            forecaster = forecaster,
                                            y          = data.production,
                                            cv         = cv,
                                            metric     = 'mean_absolute_error',
                                            verbose    = False
                                        )
metric_weighted
mean_absolute_error n_evaluated
0 27.808331 364
# Comparison of backtesting metrics
# ==============================================================================
mae_baseline = metric_baseline.loc[0, 'mean_absolute_error']
mae_weighted = metric_weighted.loc[0, 'mean_absolute_error']
results = pd.DataFrame(
              {'mean_absolute_error': [mae_baseline, mae_weighted]},
              index = ['Without weights', 'With weights']
          )
results['improvement (%)'] = 100 * (1 - results['mean_absolute_error'] / mae_baseline)
results
mean_absolute_error improvement (%)
Without weights 28.424722 0.000000
With weights 27.808331 2.168503

As in the previous example, excluding the observations of the shutdown period slightly reduces the mean absolute error, from 28.4 to 27.8 (about 2%).

Conclusions

  • The weight_func argument makes it possible to reduce or remove the influence of part of the time series during training, without breaking its continuity. The function receives the index of the training matrix and returns one weight per row.

  • When a period is excluded, the rows of the training matrix that use it as lags must be excluded as well (in this document, the following 21 days).

  • Weights only act during training, and only if the estimator accepts sample_weight. For linear models, a weight of 0 is equivalent to removing the observation; for gradient boosting models it is not exactly the same.

  • In the two examples of this document, excluding the anomalous periods reduced the mean absolute error by 2-5%. The effect of the weights should therefore be verified with backtesting in each case rather than taken for granted: it depends on the model, on how anomalous the excluded period is and on how much training data is lost.

  • In both examples the weights were used only to include or exclude observations, but any non-negative value is allowed: a weight between 0 and 1 reduces the influence of a period without removing it completely, and a weight greater than 1 increases it.

For more details about the use of weights, see the Weighted time series forecasting user guide. When part of the data is missing rather than unreliable, see Forecasting time series with missing values.

Session information

import session_info
session_info.show(html=False)
-----
matplotlib          3.11.2
numpy               2.5.4
pandas              2.3.3
session_info        v1.0.1
skforecast          0.27.0
sklearn             1.9.1
-----
IPython             9.17.1
jupyter_client      8.10.0
jupyter_core        5.9.1
-----
Python 3.13.18 | packaged by conda-forge | (main, Oct 26 2026, 11:30:40) [MSC v.1944 64 bit (AMD64)]
Windows-11-10.0.26200-SP0
-----
Session information updated at 2026-11-11 21:44

Citation

How to cite this document

If you use this document or any part of it, please acknowledge the source, thank you!

Mitigating the Impact of Covid on Forecasting Models by Joaquín Amat Rodrigo and Javier Escobar Ortiz, available under Attribution-NonCommercial-ShareAlike 4.0 International (CC BY-NC-SA 4.0 DEED) at https://www.cienciadedatos.net/documentos/py45-weighted-time-series-forecasting.html

How to cite skforecast

If you use skforecast for a publication, we would appreciate it if you cite the published software.

Zenodo:

Amat Rodrigo, Joaquin, & Escobar Ortiz, Javier. (2026). skforecast (v0.27.0). Zenodo. https://doi.org/10.5281/zenodo.8382787

APA:

Amat Rodrigo, J., & Escobar Ortiz, J. (2026). skforecast (Version 0.27.0) [Computer software]. https://doi.org/10.5281/zenodo.8382787

BibTeX:

@software{skforecast, author = {Amat Rodrigo, Joaquin and Escobar Ortiz, Javier}, title = {skforecast}, version = {0.27.0}, month = {11}, year = {2026}, license = {BSD-3-Clause}, url = {https://skforecast.org/}, doi = {10.5281/zenodo.8382787} }


Did you like the article? Your support is important

Your contribution will help me to continue generating free educational content. Many thanks! 😊

No description has been provided for this image

No description has been provided for this image

Become a GitHub Sponsor Become a GitHub Sponsor

Creative Commons Licence

This work by Joaquín Amat Rodrigo and Javier Escobar Ortiz is licensed under a Attribution-NonCommercial-ShareAlike 4.0 International.

Allowed:

  • Share: copy and redistribute the material in any medium or format.

  • Adapt: remix, transform, and build upon the material.

Under the following terms:

  • Attribution: You must give appropriate credit, provide a link to the license, and indicate if changes were made. You may do so in any reasonable manner, but not in any way that suggests the licensor endorses you or your use.

  • NonCommercial: You may not use the material for commercial purposes.

  • ShareAlike: If you remix, transform, or build upon the material, you must distribute your contributions under the same license as the original.