Skip to content

utils

skforecast.utils.utils.save_forecaster

save_forecaster(
    forecaster,
    file_name,
    backend="joblib",
    save_custom_functions=True,
    verbose=False,
    suppress_warnings=False,
)

Save forecaster model to disk. Custom functions used to create weights that are defined in the '__main__' namespace (e.g. a notebook or a script run directly) are saved as .py files, since they cannot be re-imported when the forecaster is loaded in a different session. Functions imported from a module are restored automatically and are not exported. When backend='cloudpickle', custom functions are embedded in the saved file and no .py files are created.

Parameters:

Name Type Description Default
forecaster Forecaster

Forecaster created with skforecast library.

required
file_name str

File name given to the object. The file extension is determined by the backend argument.

required
backend str

Serialization backend used to save the forecaster.

  • If 'joblib', the forecaster is saved using joblib (extension .joblib).
  • If 'pickle', the forecaster is saved using pickle (extension .pkl).
  • If 'cloudpickle', the forecaster is saved using cloudpickle (extension .cloudpickle). Custom functions and user-defined classes are embedded in the file, so no separate .py files are needed. Requires cloudpickle to be installed.
  • If 'skops', the forecaster is saved using skops (extension .skops), a secure format that does not execute arbitrary code on load. The last_window_ and training_range_ attributes are decomposed into plain types before saving and rebuilt on load, since skops cannot serialize pandas objects. Not supported for ForecasterStats, ForecasterRnn, or ForecasterFoundation, whose underlying estimators (statsmodels, Keras, or a foundation model) embed objects that skops cannot serialize. Requires skops to be installed.
'joblib'
save_custom_functions bool

If True, save custom functions used in the forecaster (weight_func) as .py files, but only those defined in the '__main__' namespace. These functions need to be available in the environment where the forecaster is going to be loaded. Has no effect when backend='cloudpickle'.

True
verbose bool

Print summary about the forecaster saved.

False
suppress_warnings bool

If True, skforecast warnings will be suppressed. See skforecast.exceptions.warn_skforecast_categories for more information.

False

Returns:

Type Description
None
Source code in skforecast/utils/utils.py
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
@manage_warnings
def save_forecaster(
    forecaster: object,
    file_name: str,
    backend: str = 'joblib',
    save_custom_functions: bool = True,
    verbose: bool = False,
    suppress_warnings: bool = False
) -> None:
    """
    Save forecaster model to disk. Custom functions used to create weights that
    are defined in the `'__main__'` namespace (e.g. a notebook or a script run
    directly) are saved as .py files, since they cannot be re-imported when the
    forecaster is loaded in a different session. Functions imported from a module
    are restored automatically and are not exported. When `backend='cloudpickle'`,
    custom functions are embedded in the saved file and no .py files are created.

    Parameters
    ----------
    forecaster : Forecaster
        Forecaster created with skforecast library.
    file_name : str
        File name given to the object. The file extension is determined by
        the `backend` argument.
    backend : str, default 'joblib'
        Serialization backend used to save the forecaster.

        - If `'joblib'`, the forecaster is saved using joblib (extension
        `.joblib`).
        - If `'pickle'`, the forecaster is saved using pickle (extension
        `.pkl`).
        - If `'cloudpickle'`, the forecaster is saved using cloudpickle
        (extension `.cloudpickle`). Custom functions and user-defined classes
        are embedded in the file, so no separate `.py` files are needed.
        Requires `cloudpickle` to be installed.
        - If `'skops'`, the forecaster is saved using skops (extension
        `.skops`), a secure format that does not execute arbitrary code on
        load. The `last_window_` and `training_range_` attributes are
        decomposed into plain types before saving and rebuilt on load, since
        skops cannot serialize pandas objects. Not supported for
        `ForecasterStats`, `ForecasterRnn`, or `ForecasterFoundation`, whose
        underlying estimators (statsmodels, Keras, or a foundation model) embed
        objects that skops cannot serialize. Requires `skops` to be installed.
    save_custom_functions : bool, default True
        If True, save custom functions used in the forecaster (weight_func) as
        .py files, but only those defined in the `'__main__'` namespace. These
        functions need to be available in the environment where the forecaster
        is going to be loaded. Has no effect when `backend='cloudpickle'`.
    verbose : bool, default False
        Print summary about the forecaster saved.
    suppress_warnings : bool, default False
        If `True`, skforecast warnings will be suppressed. See
        skforecast.exceptions.warn_skforecast_categories for more information.

    Returns
    -------
    None

    """

    valid_backends = {'joblib', 'pickle', 'cloudpickle', 'skops'}
    if backend not in valid_backends:
        raise ValueError(
            f"Invalid `backend` argument: '{backend}'. Valid options are: "
            f"{', '.join(repr(b) for b in sorted(valid_backends))}."
        )

    backend_extensions = {
        'joblib': '.joblib',
        'pickle': '.pkl',
        'cloudpickle': '.cloudpickle',
        'skops': '.skops'
    }
    file_name = Path(file_name).with_suffix(backend_extensions[backend])

    # Save forecaster
    if backend == 'joblib':
        joblib.dump(forecaster, filename=file_name)
    elif backend == 'pickle':
        with open(file_name, 'wb') as file:
            pickle.dump(forecaster, file)
    elif backend == 'cloudpickle':
        try:
            import cloudpickle
        except ImportError as exc:
            raise ImportError(
                "'cloudpickle' is required for backend='cloudpickle' but is not "
                "installed. Install it with: pip install cloudpickle"
            ) from exc
        with open(file_name, 'wb') as file:
            cloudpickle.dump(forecaster, file)
    elif backend == 'skops':
        unsupported_forecasters = {
            'ForecasterStats', 'ForecasterRnn', 'ForecasterFoundation'
        }
        if type(forecaster).__name__ in unsupported_forecasters:
            raise NotImplementedError(
                f"backend='skops' is not supported for {type(forecaster).__name__}. "
                f"Its underlying estimator (statsmodels, Keras, or a foundation "
                f"model) embeds objects that skops cannot serialize. Use "
                f"backend='joblib', 'pickle', or 'cloudpickle' instead."
            )
        try:
            import skops.io
        except ImportError as exc:
            raise ImportError(
                "'skops' is required for backend='skops' but is not installed. "
                "Install it with: pip install skops"
            ) from exc
        # NOTE: `last_window_` and `training_range_` are decomposed before the
        # dump and restored.
        originals = {
            a: getattr(forecaster, a, None)
            for a in ('last_window_', 'training_range_')
        }
        try:
            _skops_decompose_forecaster(forecaster)
            skops.io.dump(forecaster, file_name)
        finally:
            for a, v in originals.items():
                setattr(forecaster, a, v)

    if backend != 'cloudpickle':
        if hasattr(forecaster, 'weight_func') and forecaster.weight_func is not None:
            funs = (
                set(forecaster.weight_func.values())
                if isinstance(forecaster.weight_func, dict)
                else {forecaster.weight_func}
            )
            # NOTE: Only functions defined in the '__main__' namespace (notebook/script)
            # cannot be re-imported when the forecaster is loaded in a different
            # session, so they are the only ones that need the .py export / warning.
            # Functions from importable modules are restored automatically by
            # joblib/pickle (by reference).
            main_funs = sorted(
                (f for f in funs if getattr(f, '__module__', None) == '__main__'),
                key=lambda f: f.__name__
            )
            if main_funs:
                if save_custom_functions:
                    saved_files = []
                    for fun in main_funs:
                        fun_file_name = fun.__name__ + '.py'
                        with open(fun_file_name, 'w') as file:
                            file.write(inspect.getsource(fun))
                        saved_files.append(fun_file_name)
                    warnings.warn(
                        "Custom function(s) used to create weights are defined in "
                        "the '__main__' namespace and have been saved as: "
                        f"{', '.join(repr(f) for f in saved_files)}. These files "
                        "must be imported before loading the forecaster.\n"
                        "Visit the documentation for more information: "
                        "https://skforecast.org/latest/user_guides/save-load-forecaster.html"
                        "#saving-and-loading-a-forecaster-model-with-custom-features",
                        SaveLoadSkforecastWarning
                    )
                else:
                    warnings.warn(
                        "Custom function(s) used to create weights are defined in "
                        "the '__main__' namespace and have not been saved. To save "
                        "them automatically, set `save_custom_functions=True`. "
                        "Otherwise, ensure they are importable before loading the "
                        "forecaster.",
                        SaveLoadSkforecastWarning
                    )

        if hasattr(forecaster, 'window_features') and forecaster.window_features is not None:
            skforecast_classes = {'RollingFeatures'}
            custom_classes = set(forecaster.window_features_class_names) - skforecast_classes
            if custom_classes:
                warnings.warn(
                    "The Forecaster includes custom user-defined classes in the "
                    "`window_features` argument. These classes are not saved automatically "
                    "when saving the Forecaster. Please ensure you save these classes "
                    "manually and import them before loading the Forecaster.\n"
                    "    Custom classes: " + ', '.join(custom_classes) + "\n"
                    "Visit the documentation for more information: "
                    "https://skforecast.org/latest/user_guides/save-load-forecaster.html#saving-and-loading-a-forecaster-model-with-custom-features",
                    SaveLoadSkforecastWarning
                )

    if verbose:
        forecaster.summary()

skforecast.utils.utils.load_forecaster

load_forecaster(
    file_name,
    backend=None,
    trusted=False,
    verbose=True,
    suppress_warnings=False,
)

Load forecaster model from disk. If the forecaster was saved with custom user-defined classes as window features or custom functions to create weights, these objects must be available in the environment where the forecaster is going to be loaded.

Parameters:

Name Type Description Default
file_name str

Object file name.

required
backend (str, None)

Serialization backend used to load the forecaster. When None, the backend is inferred from the file extension:

  • .joblib : 'joblib'
  • .pkl or .pickle : 'pickle'
  • .cloudpickle : 'cloudpickle'
  • .skops : 'skops'
None
trusted (bool, list)

Types that skops is allowed to reconstruct when loading the file. Only used when backend='skops', ignored otherwise. Controls the trusted argument of skops.io.load:

  • If False, only skops' built-in safe types are trusted. Because all skforecast forecasters contain types that are not trusted by default, loading raises an UntrustedTypesFoundException listing them. This is the secure default: review the listed types before trusting them.
  • If a list of str, additionally trust those type names. Obtain the candidate list with skops.io.get_untrusted_types(file=file_name) and pass it after reviewing it.
  • If True, trust every type found in the file. Use this only for files from a source you trust, as it removes skops' security guarantee.

New in version 0.23.0

False
verbose bool

Print summary about the forecaster loaded.

True
suppress_warnings bool

If True, skforecast warnings will be suppressed. See skforecast.exceptions.warn_skforecast_categories for more information.

False

Returns:

Name Type Description
forecaster Forecaster

Forecaster created with skforecast library.

Source code in skforecast/utils/utils.py
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
@manage_warnings
def load_forecaster(
    file_name: str,
    backend: str | None = None,
    trusted: bool | list[str] = False,
    verbose: bool = True,
    suppress_warnings: bool = False
) -> object:
    """
    Load forecaster model from disk. If the forecaster was saved with
    custom user-defined classes as window features or custom functions
    to create weights, these objects must be available in the environment
    where the forecaster is going to be loaded.

    Parameters
    ----------
    file_name : str
        Object file name.
    backend : str, None, default None
        Serialization backend used to load the forecaster. When `None`, the
        backend is inferred from the file extension:

        - `.joblib` : `'joblib'`
        - `.pkl` or `.pickle` : `'pickle'`
        - `.cloudpickle` : `'cloudpickle'`
        - `.skops` : `'skops'`
    trusted : bool, list, default False
        Types that skops is allowed to reconstruct when loading the file. Only
        used when `backend='skops'`, ignored otherwise. Controls the `trusted`
        argument of `skops.io.load`:

        - If `False`, only skops' built-in safe types are trusted. Because all
        skforecast forecasters contain types that are not trusted by default,
        loading raises an `UntrustedTypesFoundException` listing them. This is
        the secure default: review the listed types before trusting them.
        - If a list of str, additionally trust those type names. Obtain the
        candidate list with `skops.io.get_untrusted_types(file=file_name)` and
        pass it after reviewing it.
        - If `True`, trust every type found in the file. Use this only for files
        from a source you trust, as it removes skops' security guarantee.

        **New in version 0.23.0**
    verbose : bool, default True
        Print summary about the forecaster loaded.
    suppress_warnings : bool, default False
        If `True`, skforecast warnings will be suppressed. See
        skforecast.exceptions.warn_skforecast_categories for more information.

    Returns
    -------
    forecaster : Forecaster
        Forecaster created with skforecast library.

    """

    extension_backend_map = {
        '.cloudpickle': 'cloudpickle',
        '.joblib': 'joblib',
        '.pkl': 'pickle',
        '.pickle': 'pickle',
        '.skops': 'skops',
    }
    valid_backends = {'joblib', 'pickle', 'cloudpickle', 'skops'}

    if backend is None:
        suffix = Path(file_name).suffix.lower()
        if suffix not in extension_backend_map:
            raise ValueError(
                f"Cannot infer backend from file extension '{suffix}'. "
                f"Recognized extensions: "
                f"{', '.join(repr(e) for e in sorted(extension_backend_map))}. "
                f"Provide the `backend` argument explicitly."
            )
        backend = extension_backend_map[suffix]
    elif backend not in valid_backends:
        raise ValueError(
            f"Invalid `backend` argument: '{backend}'. Valid options are: "
            f"{', '.join(repr(b) for b in sorted(valid_backends))}."
        )

    if backend == 'joblib':
        forecaster = joblib.load(filename=Path(file_name))
    elif backend == 'pickle':
        with open(file_name, 'rb') as file:
            forecaster = pickle.load(file)
    elif backend == 'cloudpickle':
        try:
            import cloudpickle  # noqa: F401 — needed to unpickle cloudpickle files
        except ImportError as exc:
            raise ImportError(
                "'cloudpickle' is required for backend='cloudpickle' but is not "
                "installed. Install it with: pip install cloudpickle"
            ) from exc
        with open(file_name, 'rb') as file:
            forecaster = pickle.load(file)
    elif backend == 'skops':
        try:
            import skops.io
            from skops.io.exceptions import UntrustedTypesFoundException
        except ImportError as exc:
            raise ImportError(
                "'skops' is required for backend='skops' but is not installed. "
                "Install it with: pip install skops"
            ) from exc
        # skops is the secure backend: by default (`trusted=False`) only its
        # built-in safe types are loaded and any other type must be reviewed and
        # trusted explicitly. `skops.io.load` only accepts a list of type names
        # (or None), so the friendly `trusted` argument is mapped here: `False`
        # -> None (strict), `True` -> all types found in the file, list -> as is.
        # `last_window_` and `training_range_` are rebuilt from the plain types
        # stored by `save_forecaster`.
        if trusted is False:
            trusted_types = None
        elif trusted is True:
            trusted_types = skops.io.get_untrusted_types(file=file_name)
        else:
            trusted_types = trusted
        try:
            forecaster = skops.io.load(file=file_name, trusted=trusted_types)
        except UntrustedTypesFoundException as exc:
            exc.args = (
                f"{exc.args[0]}\n"
                f"skops does not load these types unless you explicitly trust them. "
                f"To review the full list of untrusted types in the file, run "
                f"`skops.io.get_untrusted_types(file='{file_name}')`. If you trust the "
                f"source of '{file_name}', reload with `load_forecaster(..., trusted=True)` "
                f"to trust them all, or pass the reviewed list via `trusted=[...]`.",
            )
            raise
        _skops_reconstruct_forecaster(forecaster)

    forecaster_v = forecaster.skforecast_version

    if forecaster_v != __version__:
        warnings.warn(
            f"The skforecast version installed in the environment differs "
            f"from the version used to create the forecaster.\n"
            f"    Installed Version  : {__version__}\n"
            f"    Forecaster Version : {forecaster_v}\n"
            f"This may create incompatibilities when using the library.",
            SkforecastVersionWarning
        )

    if verbose:
        forecaster.summary()

    return forecaster

skforecast.utils.utils.initialize_lags

initialize_lags(forecaster_name, lags)

Check lags argument input and generate the corresponding numpy ndarray.

Parameters:

Name Type Description Default
forecaster_name str

Forecaster name.

required
lags Any

Lags used as predictors.

required

Returns:

Name Type Description
lags numpy ndarray, None

Lags used as predictors.

lags_names (list, None)

Names of the lags used as predictors.

max_lag (int, None)

Maximum value of the lags.

Source code in skforecast/utils/utils.py
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
def initialize_lags(
    forecaster_name: str,
    lags: Any
) -> tuple[np.ndarray[int] | None, list[str] | None, int | None]:
    """
    Check lags argument input and generate the corresponding numpy ndarray.

    Parameters
    ----------
    forecaster_name : str
        Forecaster name.
    lags : Any
        Lags used as predictors.

    Returns
    -------
    lags : numpy ndarray, None
        Lags used as predictors.
    lags_names : list, None
        Names of the lags used as predictors.
    max_lag : int, None
        Maximum value of the lags.

    """

    lags_names = None
    max_lag = None
    if lags is not None:
        if isinstance(lags, int):
            if lags < 1:
                raise ValueError("Minimum value of lags allowed is 1.")
            lags = np.arange(1, lags + 1)

        if isinstance(lags, (list, tuple, range)):
            lags = np.array(lags)

        if isinstance(lags, np.ndarray):
            if lags.size == 0:
                return None, None, None
            if lags.ndim != 1:
                raise ValueError("`lags` must be a 1-dimensional array.")
            if not np.issubdtype(lags.dtype, np.integer):
                raise TypeError("All values in `lags` must be integers.")
            if np.any(lags < 1):
                raise ValueError("Minimum value of lags allowed is 1.")
        else:
            if forecaster_name == 'ForecasterDirectMultiVariate':
                raise TypeError(
                    f"`lags` argument must be a dict, int, 1d numpy ndarray, range, "
                    f"tuple or list. Got {type(lags)}."
                )
            else:
                raise TypeError(
                    f"`lags` argument must be an int, 1d numpy ndarray, range, "
                    f"tuple or list. Got {type(lags)}."
                )

        lags = np.sort(lags)
        lags_names = [f'lag_{i}' for i in lags]
        max_lag = max(lags)

    return lags, lags_names, max_lag

skforecast.utils.utils.initialize_weights

initialize_weights(
    forecaster_name, estimator, weight_func, series_weights
)

Check weights arguments, weight_func and series_weights for the different forecasters. Create source_code_weight_func, source code of the custom function(s) used to create weights.

Parameters:

Name Type Description Default
forecaster_name str

Forecaster name.

required
estimator estimator or pipeline compatible with the scikit-learn API

Estimator of the forecaster.

required
weight_func (Callable, dict)

Argument weight_func of the forecaster.

required
series_weights dict

Argument series_weights of the forecaster.

required

Returns:

Name Type Description
weight_func (Callable, dict)

Argument weight_func of the forecaster.

source_code_weight_func (str, dict)

Argument source_code_weight_func of the forecaster.

series_weights dict

Argument series_weights of the forecaster. Only ForecasterRecursiveMultiSeries.

Source code in skforecast/utils/utils.py
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
def initialize_weights(
    forecaster_name: str,
    estimator: object,
    weight_func: Callable | dict[str, Callable],
    series_weights: dict[str, float]
) -> tuple[Callable | dict[str, Callable] | None, str | dict[str, str] | None, dict[str, float] | None]:
    """
    Check weights arguments, `weight_func` and `series_weights` for the different 
    forecasters. Create `source_code_weight_func`, source code of the custom 
    function(s) used to create weights.

    Parameters
    ----------
    forecaster_name : str
        Forecaster name.
    estimator : estimator or pipeline compatible with the scikit-learn API
        Estimator of the forecaster.
    weight_func : Callable, dict
        Argument `weight_func` of the forecaster.
    series_weights : dict
        Argument `series_weights` of the forecaster.

    Returns
    -------
    weight_func : Callable, dict
        Argument `weight_func` of the forecaster.
    source_code_weight_func : str, dict
        Argument `source_code_weight_func` of the forecaster.
    series_weights : dict
        Argument `series_weights` of the forecaster. Only ForecasterRecursiveMultiSeries.

    """

    source_code_weight_func = None

    if weight_func is not None:

        if forecaster_name in ['ForecasterRecursiveMultiSeries']:
            if not isinstance(weight_func, (Callable, dict)):
                raise TypeError(
                    f"Argument `weight_func` must be a Callable or a dict of "
                    f"Callables. Got {type(weight_func)}."
                )
        elif not isinstance(weight_func, Callable):
            raise TypeError(
                f"Argument `weight_func` must be a Callable. Got {type(weight_func)}."
            )

        if isinstance(weight_func, dict):
            source_code_weight_func = {}
            for key in weight_func:
                source_code_weight_func[key] = inspect.getsource(weight_func[key])
        else:
            source_code_weight_func = inspect.getsource(weight_func)

        if 'sample_weight' not in inspect.signature(estimator.fit).parameters:
            warnings.warn(
                f"Argument `weight_func` is ignored since estimator {estimator} "
                f"does not accept `sample_weight` in its `fit` method.",
                IgnoredArgumentWarning
            )
            weight_func = None
            source_code_weight_func = None

    if series_weights is not None:
        if not isinstance(series_weights, dict):
            raise TypeError(
                f"Argument `series_weights` must be a dict of floats or ints."
                f"Got {type(series_weights)}."
            )
        if 'sample_weight' not in inspect.signature(estimator.fit).parameters:
            warnings.warn(
                f"Argument `series_weights` is ignored since estimator {estimator} "
                f"does not accept `sample_weight` in its `fit` method.",
                IgnoredArgumentWarning
            )
            series_weights = None

    return weight_func, source_code_weight_func, series_weights

skforecast.utils.utils.initialize_transformer_series

initialize_transformer_series(
    forecaster_name,
    series_names_in_,
    encoding=None,
    transformer_series=None,
)

Initialize transformer_series_ attribute for the Forecasters Multiseries.

  • If transformer_series is None, no transformation is applied.
  • If transformer_series is a scikit-learn transformer (object), the same transformer is applied to all series (series_names_in_).
  • If transformer_series is a dict, a different transformer can be applied to each series. The keys of the dictionary must be the same as the names of the series in series_names_in_.

Parameters:

Name Type Description Default
forecaster_name str

Forecaster name.

required
series_names_in_ list

Names of the series (levels) used during training.

required
encoding str

Encoding used to identify the different series (ForecasterRecursiveMultiSeries).

None
transformer_series (object, dict)

An instance of a transformer (preprocessor) compatible with the scikit-learn preprocessing API with methods: fit, transform, fit_transform and inverse_transform.

None

Returns:

Name Type Description
transformer_series_ dict

Dictionary with the transformer for each series. It is created cloning the objects in transformer_series and is used internally to avoid overwriting.

Source code in skforecast/utils/utils.py
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
def initialize_transformer_series(
    forecaster_name: str,
    series_names_in_: list[str],
    encoding: str | None = None,
    transformer_series: object | dict[str, object | None] | None = None
) -> dict[str, object | None]:
    """
    Initialize `transformer_series_` attribute for the Forecasters Multiseries.

    - If `transformer_series` is `None`, no transformation is applied.
    - If `transformer_series` is a scikit-learn transformer (object), the same 
    transformer is applied to all series (`series_names_in_`).
    - If `transformer_series` is a `dict`, a different transformer can be
    applied to each series. The keys of the dictionary must be the same as the
    names of the series in `series_names_in_`.

    Parameters
    ----------
    forecaster_name : str
        Forecaster name.
    series_names_in_ : list
        Names of the series (levels) used during training.
    encoding : str, default None
        Encoding used to identify the different series (`ForecasterRecursiveMultiSeries`).
    transformer_series : object, dict, default None
        An instance of a transformer (preprocessor) compatible with the scikit-learn
        preprocessing API with methods: fit, transform, fit_transform and 
        inverse_transform. 

    Returns
    -------
    transformer_series_ : dict
        Dictionary with the transformer for each series. It is created cloning the 
        objects in `transformer_series` and is used internally to avoid overwriting.

    """

    if forecaster_name == 'ForecasterRecursiveMultiSeries':
        if encoding is None:
            series_names_in_ = ['_unknown_level']
        else:
            series_names_in_ = series_names_in_ + ['_unknown_level']

    if transformer_series is None:
        transformer_series_ = {serie: None for serie in series_names_in_}
    elif not isinstance(transformer_series, dict):
        transformer_series_ = {
            serie: clone(transformer_series) 
            for serie in series_names_in_
        }
    else:
        transformer_series_ = {serie: None for serie in series_names_in_}
        # Only elements already present in transformer_series_ are updated
        transformer_series_.update(
            {
                k: deepcopy(v)
                for k, v in transformer_series.items()
                if k in transformer_series_
            }
        )

        series_not_in_transformer_series = (
            set(series_names_in_) - set(transformer_series.keys())
        ) - {'_unknown_level'}
        if series_not_in_transformer_series:
            warnings.warn(
                f"{series_not_in_transformer_series} not present in `transformer_series`."
                f" No transformation is applied to these series.",
                IgnoredArgumentWarning
            )

    return transformer_series_

skforecast.utils.utils.check_select_fit_kwargs

check_select_fit_kwargs(estimator, fit_kwargs=None)

Check if fit_kwargs is a dict and select only the keys that are used by the fit method of the estimator.

Parameters:

Name Type Description Default
estimator object

Estimator object.

required
fit_kwargs dict

Dictionary with the arguments to pass to the `fit' method of the forecaster.

None

Returns:

Name Type Description
fit_kwargs dict

Dictionary with the arguments to be passed to the fit method of the estimator after removing the unused keys.

Source code in skforecast/utils/utils.py
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
def check_select_fit_kwargs(
    estimator: object,
    fit_kwargs: dict[str, object] | None = None
) -> dict[str, object]:
    """
    Check if `fit_kwargs` is a dict and select only the keys that are used by
    the `fit` method of the estimator.

    Parameters
    ----------
    estimator : object
        Estimator object.
    fit_kwargs : dict, default None
        Dictionary with the arguments to pass to the `fit' method of the forecaster.

    Returns
    -------
    fit_kwargs : dict
        Dictionary with the arguments to be passed to the `fit` method of the 
        estimator after removing the unused keys.

    """

    if fit_kwargs is None:
        fit_kwargs = {}
    else:
        if not isinstance(fit_kwargs, dict):
            raise TypeError(
                f"Argument `fit_kwargs` must be a dict. Got {type(fit_kwargs)}."
            )

        fit_params = inspect.signature(estimator.fit).parameters

        # Non used keys
        non_used_keys = [
            k for k in fit_kwargs.keys() if k not in fit_params
        ]
        if non_used_keys:
            warnings.warn(
                f"Argument/s {non_used_keys} ignored since they are not used by the "
                f"estimator's `fit` method.",
                IgnoredArgumentWarning
            )

        if 'sample_weight' in fit_kwargs.keys():
            warnings.warn(
                "The `sample_weight` argument is ignored. Use `weight_func` to pass "
                "a function that defines the individual weights for each sample "
                "based on its index.",
                IgnoredArgumentWarning
            )
            del fit_kwargs['sample_weight']

        # Select only the keyword arguments allowed by the estimator's `fit` method.
        fit_kwargs = {
            k: v for k, v in fit_kwargs.items() if k in fit_params
        }

    return fit_kwargs

skforecast.utils.utils.check_y

check_y(y, series_id='`y`', allow_nan=False)

Raise Exception if y is not pandas Series or if it has missing values.

Parameters:

Name Type Description Default
y Any

Time series values.

required
series_id str

Identifier of the series used in the warning message.

'`y`'
allow_nan bool

If True, skip the check for missing values.

False

Returns:

Type Description
None
Source code in skforecast/utils/utils.py
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
def check_y(
    y: Any,
    series_id: str = "`y`",
    allow_nan: bool = False
) -> None:
    """
    Raise Exception if `y` is not pandas Series or if it has missing values.

    Parameters
    ----------
    y : Any
        Time series values.
    series_id : str, default '`y`'
        Identifier of the series used in the warning message.
    allow_nan : bool, default False
        If `True`, skip the check for missing values.

    Returns
    -------
    None

    """

    if not isinstance(y, pd.Series):
        raise TypeError(
            f"{series_id} must be a pandas Series with a DatetimeIndex or a RangeIndex. "
            f"Found {type(y)}."
        )

    if not allow_nan:
        if y.isna().to_numpy().any():
            raise ValueError(f"{series_id} has missing values.")

    return

skforecast.utils.utils.check_exog

check_exog(exog, allow_nan=True, series_id='`exog`')

Raise Exception if exog is not pandas Series or pandas DataFrame. If allow_nan = True, issue a warning if exog contains NaN values.

Parameters:

Name Type Description Default
exog pandas Series, pandas DataFrame

Exogenous variable/s included as predictor/s.

required
allow_nan bool

If True, allows the presence of NaN values in exog. If False (default), issue a warning if exog contains NaN values.

True
series_id str

Identifier of the series for which the exogenous variable/s are used in the warning message.

'`exog`'

Returns:

Type Description
None
Source code in skforecast/utils/utils.py
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
def check_exog(
    exog: pd.Series | pd.DataFrame,
    allow_nan: bool = True,
    series_id: str = "`exog`"
) -> None:
    """
    Raise Exception if `exog` is not pandas Series or pandas DataFrame.
    If `allow_nan = True`, issue a warning if `exog` contains NaN values.

    Parameters
    ----------
    exog : pandas Series, pandas DataFrame
        Exogenous variable/s included as predictor/s.
    allow_nan : bool, default True
        If True, allows the presence of NaN values in `exog`. If False (default),
        issue a warning if `exog` contains NaN values.
    series_id : str, default '`exog`'
        Identifier of the series for which the exogenous variable/s are used
        in the warning message.

    Returns
    -------
    None

    """

    if not isinstance(exog, (pd.Series, pd.DataFrame)):
        raise TypeError(
            f"{series_id} must be a pandas Series or DataFrame. Got {type(exog)}."
        )

    if isinstance(exog, pd.Series) and exog.name is None:
        raise ValueError(f"When {series_id} is a pandas Series, it must have a name.")

    if not allow_nan:
        if exog.isna().to_numpy().any():
            warnings.warn(
                f"{series_id} has missing values. Most machine learning models "
                f"do not allow missing values. Fitting the forecaster may fail.", 
                MissingValuesWarning
            )

    return

skforecast.utils.utils.get_exog_dtypes

get_exog_dtypes(exog)

Store dtypes of exog.

Parameters:

Name Type Description Default
exog pandas Series, pandas DataFrame

Exogenous variable/s included as predictor/s.

required

Returns:

Name Type Description
exog_dtypes dict

Dictionary with the dtypes in exog.

Source code in skforecast/utils/utils.py
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
def get_exog_dtypes(
    exog: pd.Series | pd.DataFrame, 
) -> dict[str, type]:
    """
    Store dtypes of `exog`.

    Parameters
    ----------
    exog : pandas Series, pandas DataFrame
        Exogenous variable/s included as predictor/s.

    Returns
    -------
    exog_dtypes : dict
        Dictionary with the dtypes in `exog`.

    """

    if isinstance(exog, pd.Series):
        exog_dtypes = {exog.name: exog.dtypes}
    else:
        exog_dtypes = exog.dtypes.to_dict()

    return exog_dtypes

skforecast.utils.utils.check_exog_dtypes

check_exog_dtypes(
    exog, call_check_exog=True, series_id="`exog`"
)

Raise Exception if exog has categorical columns with non integer values. This is needed when using machine learning estimators that allow categorical features. Issue a Warning if exog has columns that are not int, float, or category.

Parameters:

Name Type Description Default
exog pandas Series, pandas DataFrame

Exogenous variable/s included as predictor/s.

required
call_check_exog bool

If True, call check_exog function.

True
series_id str

Identifier of the series for which the exogenous variable/s are used in the warning message.

'`exog`'

Returns:

Type Description
None
Source code in skforecast/utils/utils.py
 939
 940
 941
 942
 943
 944
 945
 946
 947
 948
 949
 950
 951
 952
 953
 954
 955
 956
 957
 958
 959
 960
 961
 962
 963
 964
 965
 966
 967
 968
 969
 970
 971
 972
 973
 974
 975
 976
 977
 978
 979
 980
 981
 982
 983
 984
 985
 986
 987
 988
 989
 990
 991
 992
 993
 994
 995
 996
 997
 998
 999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
def check_exog_dtypes(
    exog: pd.Series | pd.DataFrame,
    call_check_exog: bool = True,
    series_id: str = "`exog`"
) -> None:
    """
    Raise Exception if `exog` has categorical columns with non integer values.
    This is needed when using machine learning estimators that allow categorical
    features.
    Issue a Warning if `exog` has columns that are not `int`, `float`, or `category`.

    Parameters
    ----------
    exog : pandas Series, pandas DataFrame
        Exogenous variable/s included as predictor/s.
    call_check_exog : bool, default True
        If `True`, call `check_exog` function.
    series_id : str, default '`exog`'
        Identifier of the series for which the exogenous variable/s are used
        in the warning message.

    Returns
    -------
    None

    """

    if call_check_exog:
        check_exog(exog=exog, allow_nan=False, series_id=series_id)

    valid_dtypes = ("int", "Int", "float", "Float", "uint")

    if isinstance(exog, pd.DataFrame):
        unique_dtypes = set(exog.dtypes)
        has_invalid_dtype = False
        for dtype in unique_dtypes:
            if isinstance(dtype, pd.CategoricalDtype):
                try:
                    is_integer = np.issubdtype(dtype.categories.dtype, np.integer)
                except TypeError:
                    is_integer = False
                if not is_integer:
                    raise TypeError(
                        "Categorical dtypes in exog must contain only integer values. "
                        "See skforecast docs for more info about how to include "
                        "categorical features https://skforecast.org/"
                        "latest/user_guides/categorical-features.html"
                    )
            elif not dtype.name.startswith(valid_dtypes):
                has_invalid_dtype = True

        if has_invalid_dtype:
            warnings.warn(
                f"{series_id} may contain only `int`, `float` or `category` dtypes. "
                f"Most machine learning models do not allow other types of values. "
                f"Fitting the forecaster may fail.", 
                DataTypeWarning
            )

    else:

        dtype_name = str(exog.dtypes)
        if not (dtype_name.startswith(valid_dtypes) or dtype_name == "category"):
            warnings.warn(
                f"{series_id} may contain only `int`, `float` or `category` dtypes. Most "
                f"machine learning models do not allow other types of values. "
                f"Fitting the forecaster may fail.", 
                DataTypeWarning
            )

        if isinstance(exog.dtype, pd.CategoricalDtype):
            try:
                is_integer = np.issubdtype(exog.cat.categories.dtype, np.integer)
            except TypeError:
                is_integer = False
            if not is_integer:
                raise TypeError(
                    "Categorical dtypes in exog must contain only integer values. "
                    "See skforecast docs for more info about how to include "
                    "categorical features https://skforecast.org/"
                    "latest/user_guides/categorical-features.html"
                )

skforecast.utils.utils.check_interval

check_interval(
    interval=None,
    ensure_symmetric_intervals=False,
    quantiles=None,
    alpha=None,
    alpha_literal="alpha",
)

Check provided confidence interval sequence is valid.

Parameters:

Name Type Description Default
interval (list, tuple)

Confidence of the prediction interval estimated. Sequence of bounds to compute. Values must be between 0 and 1 inclusive. For example, interval of 95% should be as interval = [0.025, 0.975].

None
ensure_symmetric_intervals bool

If True, ensure that the intervals are symmetric.

False
quantiles (list, tuple)

Sequence of quantiles to compute, which must be between 0 and 1 inclusive. For example, quantiles of 0.05, 0.5 and 0.95 should be as quantiles = [0.05, 0.5, 0.95].

None
alpha float

The confidence intervals used in ForecasterStats are (1 - alpha) %.

None
alpha_literal str

Literal used in the exception message when alpha is provided.

'alpha'

Returns:

Type Description
None
Source code in skforecast/utils/utils.py
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
def check_interval(
    interval: list[float] | tuple[float] | None = None,
    ensure_symmetric_intervals: bool = False,
    quantiles: list[float] | tuple[float] | None = None,
    alpha: float = None,
    alpha_literal: str | None = 'alpha'
) -> None:
    """
    Check provided confidence interval sequence is valid.

    Parameters
    ----------
    interval : list, tuple, default None
        Confidence of the prediction interval estimated. Sequence of bounds to
        compute. Values must be between 0 and 1 inclusive. For example, interval
        of 95% should be as `interval = [0.025, 0.975]`.
    ensure_symmetric_intervals : bool, default False
        If True, ensure that the intervals are symmetric.
    quantiles : list, tuple, default None
        Sequence of quantiles to compute, which must be between 0 and 1 
        inclusive. For example, quantiles of 0.05, 0.5 and 0.95 should be as 
        `quantiles = [0.05, 0.5, 0.95]`.
    alpha : float, default None
        The confidence intervals used in ForecasterStats are (1 - alpha) %.
    alpha_literal : str, default 'alpha'
        Literal used in the exception message when `alpha` is provided.

    Returns
    -------
    None

    """

    if interval is not None:
        if not isinstance(interval, (list, tuple)):
            raise TypeError(
                "`interval` must be a `list` or `tuple`. For example, interval of 95% "
                "should be as `interval = [0.025, 0.975]`."
            )

        if len(interval) != 2:
            raise ValueError(
                "`interval` must contain exactly 2 values, respectively the "
                "lower and upper interval bounds. For example, interval of 95% "
                "should be as `interval = [0.025, 0.975]`."
            )

        if (interval[0] < 0.) or (interval[0] >= 1.):
            raise ValueError(
                f"Lower interval bound ({interval[0]}) must be >= 0 and < 1."
            )

        if (interval[1] <= 0.) or (interval[1] > 1.):
            raise ValueError(
                f"Upper interval bound ({interval[1]}) must be > 0 and <= 1."
            )

        if interval[0] >= interval[1]:
            raise ValueError(
                f"Lower interval bound ({interval[0]}) must be less than the "
                f"upper interval bound ({interval[1]})."
            )

        if ensure_symmetric_intervals and interval[0] + interval[1] != 1.:
            raise ValueError(
                f"Interval must be symmetric, the sum of the lower, ({interval[0]}), "
                f"and upper, ({interval[1]}), interval bounds must be equal to "
                f"1. Got {interval[0] + interval[1]}."
            )

    if quantiles is not None:
        if not isinstance(quantiles, (list, tuple)):
            raise TypeError(
                "`quantiles` must be a `list` or `tuple`. For example, quantiles "
                "0.05, 0.5, and 0.95 should be as `quantiles = [0.05, 0.5, 0.95]`."
            )

        for q in quantiles:
            if (q < 0.) or (q > 1.):
                raise ValueError(
                    "All elements in `quantiles` must be >= 0 and <= 1."
                )

    if alpha is not None:
        if not isinstance(alpha, float):
            raise TypeError(
                f"`{alpha_literal}` must be a `float`. For example, interval of 95% "
                f"should be as `alpha = 0.05`."
            )

        if (alpha <= 0.) or (alpha >= 1):
            raise ValueError(
                f"`{alpha_literal}` must have a value between 0 and 1. Got {alpha}."
            )

skforecast.utils.utils.check_predict_input

check_predict_input(
    forecaster_name,
    steps,
    is_fitted,
    exog_in_,
    index_type_,
    index_freq_,
    window_size,
    last_window,
    last_window_exog=None,
    exog=None,
    exog_names_in_=None,
    max_step=None,
    levels=None,
    levels_forecaster=None,
    series_names_in_=None,
    encoding=None,
)

Check all inputs of predict method. This is a helper function to validate that inputs used in predict method match attributes of a forecaster already trained.

Parameters:

Name Type Description Default
forecaster_name str

Forecaster name.

required
steps (int, list)

Number of future steps predicted.

required
is_fitted bool

Tag to identify if the estimator has been fitted (trained).

required
exog_in_ bool

If the forecaster has been trained using exogenous variable/s.

required
index_type_ type

Type of index of the input used in training.

required
index_freq_ str

Frequency of Index of the input used in training.

required
window_size int

Size of the window needed to create the predictors. It is equal to max_lag.

required
last_window pandas Series, pandas DataFrame, None

Values of the series used to create the predictors (lags) need in the first iteration of prediction (t + 1).

required
last_window_exog pandas Series, pandas DataFrame

Values of the exogenous variables aligned with last_window in ForecasterStats predictions.

None
exog pandas Series, pandas DataFrame, dict

Exogenous variable/s included as predictor/s.

None
exog_names_in_ list

Names of the exogenous variables used during training.

None
max_step int | None

Maximum number of steps allowed (ForecasterDirect and ForecasterDirectMultiVariate).

None
levels (str, list)

Time series to be predicted (ForecasterRecursiveMultiSeries and `ForecasterRnn).

None
levels_forecaster (str, list)

Time series used as output data of a multiseries problem in a RNN problem (ForecasterRnn).

None
series_names_in_ list

Names of the columns used during fit (ForecasterRecursiveMultiSeries, ForecasterDirectMultiVariate and ForecasterRnn).

None
encoding str

Encoding used to identify the different series (ForecasterRecursiveMultiSeries).

None

Returns:

Type Description
None
Source code in skforecast/utils/utils.py
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
def check_predict_input(
    forecaster_name: str,
    steps: int | list[int],
    is_fitted: bool,
    exog_in_: bool,
    index_type_: type,
    index_freq_: str,
    window_size: int,
    last_window: pd.Series | pd.DataFrame | None,
    last_window_exog: pd.Series | pd.DataFrame | None = None,
    exog: pd.Series | pd.DataFrame | dict[str, pd.Series | pd.DataFrame] | None = None,
    exog_names_in_: list[str] | None = None,
    max_step: int | None = None,
    levels: str | list[str] | None = None,
    levels_forecaster: str | list[str] | None = None,
    series_names_in_: list[str] | None = None,
    encoding: str | None = None
) -> None:
    """
    Check all inputs of predict method. This is a helper function to validate
    that inputs used in predict method match attributes of a forecaster already
    trained.

    Parameters
    ----------
    forecaster_name : str
        Forecaster name.
    steps : int, list
        Number of future steps predicted.
    is_fitted: bool
        Tag to identify if the estimator has been fitted (trained).
    exog_in_ : bool
        If the forecaster has been trained using exogenous variable/s.
    index_type_ : type
        Type of index of the input used in training.
    index_freq_ : str
        Frequency of Index of the input used in training.
    window_size: int
        Size of the window needed to create the predictors. It is equal to 
        `max_lag`.
    last_window : pandas Series, pandas DataFrame, None
        Values of the series used to create the predictors (lags) need in the 
        first iteration of prediction (t + 1).
    last_window_exog : pandas Series, pandas DataFrame, default None
        Values of the exogenous variables aligned with `last_window` in 
        ForecasterStats predictions.
    exog : pandas Series, pandas DataFrame, dict, default None
        Exogenous variable/s included as predictor/s.
    exog_names_in_ : list, default None
        Names of the exogenous variables used during training.
    max_step: int, default None
        Maximum number of steps allowed (`ForecasterDirect` and 
        `ForecasterDirectMultiVariate`).
    levels : str, list, default None
        Time series to be predicted (`ForecasterRecursiveMultiSeries`
        and `ForecasterRnn).
    levels_forecaster : str, list, default None
        Time series used as output data of a multiseries problem in a RNN problem
        (`ForecasterRnn`).
    series_names_in_ : list, default None
        Names of the columns used during fit (`ForecasterRecursiveMultiSeries`, 
        `ForecasterDirectMultiVariate` and `ForecasterRnn`).
    encoding : str, default None
        Encoding used to identify the different series (`ForecasterRecursiveMultiSeries`).

    Returns
    -------
    None

    """

    if not is_fitted:
        raise NotFittedError(
            "This Forecaster instance is not fitted yet. Call `fit` with "
            "appropriate arguments before using predict."
        )

    if isinstance(steps, (int, np.integer)) and steps < 1:
        raise ValueError(
            f"`steps` must be an integer greater than or equal to 1. Got {steps}."
        )

    if isinstance(steps, list) and min(steps) < 1:
        raise ValueError(
           f"The minimum value of `steps` must be equal to or greater than 1. "
           f"Got {min(steps)}."
        )

    if max_step is not None:
        if max(steps) > max_step:
            raise ValueError(
                f"The maximum value of `steps` must be less than or equal to "
                f"the value of steps defined when initializing the forecaster. "
                f"Got {max(steps)}, but the maximum is {max_step}."
            )

    if forecaster_name in ['ForecasterRecursiveMultiSeries', 'ForecasterRnn']:
        if not isinstance(levels, (type(None), str, list)):
            raise TypeError(
                "`levels` must be a `list` of column names, a `str` of a "
                "column name or `None`."
            )

        levels_to_check = (
            levels_forecaster if forecaster_name == 'ForecasterRnn'
            else series_names_in_
        )
        unknown_levels = set(levels) - set(levels_to_check)
        if forecaster_name == 'ForecasterRnn':
            if len(unknown_levels) != 0:
                raise ValueError(
                    f"`levels` names must be included in the series used during fit "
                    f"({levels_to_check}). Got {levels}."
                )
        else:
            if len(unknown_levels) != 0 and last_window is not None and encoding is not None:
                if encoding == 'onehot':
                    warnings.warn(
                        f"`levels` {unknown_levels} were not included in training. The resulting "
                        f"one-hot encoded columns for this feature will be all zeros.",
                        UnknownLevelWarning
                    )
                else:
                    warnings.warn(
                        f"`levels` {unknown_levels} were not included in training. "
                        f"Unknown levels are encoded as NaN, which may cause the "
                        f"prediction to fail if the estimator does not accept NaN values.",
                        UnknownLevelWarning
                    )

    if exog is None and exog_in_:
        raise ValueError(
            "Forecaster trained with exogenous variable/s. "
            "Same variable/s must be provided when predicting."
        )

    if exog is not None and not exog_in_:
        raise ValueError(
            "Forecaster trained without exogenous variable/s. "
            "`exog` must be `None` when predicting."
        )

    # Checks last_window
    # Check last_window type (pd.Series or pd.DataFrame according to forecaster)
    if isinstance(last_window, type(None)) and forecaster_name not in [
        'ForecasterRecursiveMultiSeries', 
        'ForecasterRnn'
    ]:
        raise ValueError(
            "`last_window` was not stored during training. If you don't want "
            "to retrain the Forecaster, provide `last_window` as argument."
        )

    if forecaster_name in [
        'ForecasterRecursiveMultiSeries', 
        'ForecasterDirectMultiVariate',
        'ForecasterRnn'
    ]:
        if not isinstance(last_window, pd.DataFrame):
            raise TypeError(
                f"`last_window` must be a pandas DataFrame. Got {type(last_window)}."
            )

        last_window_cols = last_window.columns.to_list()

        if (
            forecaster_name in ["ForecasterRecursiveMultiSeries", "ForecasterRnn"]
            and len(set(levels) - set(last_window_cols)) != 0
        ):
            missing_levels = set(levels) - set(last_window_cols)
            raise ValueError(
                f"`last_window` must contain a column(s) named as the level(s) to be predicted. "
                f"The following `levels` are missing in `last_window`: {missing_levels}\n"
                f"Ensure that `last_window` contains all the necessary columns "
                f"corresponding to the `levels` being predicted.\n"
                f"    Argument `levels`     : {levels}\n"
                f"    `last_window` columns : {last_window_cols}\n"
                f"Example: If `levels = ['series_1', 'series_2']`, make sure "
                f"`last_window` includes columns named 'series_1' and 'series_2'."
            )

        if forecaster_name == 'ForecasterDirectMultiVariate':
            if len(set(series_names_in_) - set(last_window_cols)) > 0:
                raise ValueError(
                    f"`last_window` columns must be the same as the `series` "
                    f"column names used to create the X_train matrix.\n"
                    f"    `last_window` columns    : {last_window_cols}\n"
                    f"    `series` columns X train : {series_names_in_}"
                )
    else:
        if not isinstance(last_window, (pd.Series, pd.DataFrame)):
            raise TypeError(
                f"`last_window` must be a pandas Series or DataFrame. "
                f"Got {type(last_window)}."
            )

    # Check last_window len, nulls and index (type and freq)
    if len(last_window) < window_size:
        raise ValueError(
            f"`last_window` must have as many values as needed to "
            f"generate the predictors. For this forecaster it is {window_size}."
        )
    if last_window.isna().to_numpy().any():
        warnings.warn(
            "`last_window` has missing values. Most of machine learning models do "
            "not allow missing values. Prediction method may fail.", 
            MissingValuesWarning
        )

    _, last_window_index = check_extract_values_and_index(
        data=last_window, data_label='`last_window`', ignore_freq=False, return_values=False
    )
    if not isinstance(last_window_index, index_type_):
        raise TypeError(
            f"Expected index of type {index_type_} for `last_window`. "
            f"Got {type(last_window_index)}."
        )
    if isinstance(last_window_index, pd.DatetimeIndex):
        if not last_window_index.freq == index_freq_:
            raise TypeError(
                f"Expected frequency of type {index_freq_} for `last_window`. "
                f"Got {last_window_index.freq}."
            )
    else:
        if not last_window_index.step == index_freq_:
            raise TypeError(
                f"Expected step of type {index_freq_} for `last_window`. "
                f"Got {last_window_index.step}."
            )

    # Checks exog
    if exog is not None:

        # Check type, nulls and expected type
        if forecaster_name in ['ForecasterRecursiveMultiSeries']:
            if not isinstance(exog, (pd.Series, pd.DataFrame, dict)):
                raise TypeError(
                    f"`exog` must be a pandas Series, DataFrame or dict. Got {type(exog)}."
                )
        else:
            if not isinstance(exog, (pd.Series, pd.DataFrame)):
                raise TypeError(
                    f"`exog` must be a pandas Series or DataFrame. Got {type(exog)}."
                )

        if isinstance(exog, dict):
            no_exog_levels = set(levels) - set(exog.keys())
            if no_exog_levels:
                warnings.warn(
                    f"`exog` does not contain keys for levels {no_exog_levels}. "
                    f"Missing levels are filled with NaN. Most of machine learning "
                    f"models do not allow missing values. Prediction method may fail.",
                    MissingExogWarning
                )
            exogs_to_check = [
                (f"`exog` for series '{k}'", v) 
                for k, v in exog.items() 
                if v is not None and k in levels
            ]
        else:
            exogs_to_check = [('`exog`', exog)]

        last_step = max(steps) if isinstance(steps, list) else steps
        expected_index = expand_index(last_window_index, 1)[0]
        for exog_name, exog_to_check in exogs_to_check:

            if not isinstance(exog_to_check, (pd.Series, pd.DataFrame)):
                raise TypeError(
                    f"{exog_name} must be a pandas Series or DataFrame. Got {type(exog_to_check)}"
                )

            if exog_to_check.isna().to_numpy().any():
                warnings.warn(
                    f"{exog_name} has missing values. Most of machine learning models "
                    f"do not allow missing values. Prediction method may fail.", 
                    MissingValuesWarning
                )

            # Check exog has many values as distance to max step predicted
            if len(exog_to_check) < last_step:
                if forecaster_name in ['ForecasterRecursiveMultiSeries']:
                    warnings.warn(
                        f"{exog_name} doesn't have as many values as steps "
                        f"predicted, {last_step}. Missing values are filled "
                        f"with NaN. Most of machine learning models do not "
                        f"allow missing values. Prediction method may fail.",
                        MissingValuesWarning
                    )
                else: 
                    raise ValueError(
                        f"{exog_name} must have at least as many values as "
                        f"steps predicted, {last_step}."
                    )

            # Check name/columns are in exog_names_in_
            if isinstance(exog_to_check, pd.DataFrame):
                col_missing = set(exog_names_in_).difference(set(exog_to_check.columns))
                if col_missing:
                    if forecaster_name in ['ForecasterRecursiveMultiSeries']:
                        warnings.warn(
                            f"{col_missing} not present in {exog_name}. All "
                            f"values will be NaN.",
                            MissingExogWarning
                        ) 
                    else:
                        raise ValueError(
                            f"Missing columns in {exog_name}. Expected {exog_names_in_}. "
                            f"Got {exog_to_check.columns.to_list()}."
                        )
            else:
                if exog_to_check.name is None:
                    raise ValueError(
                        f"When {exog_name} is a pandas Series, it must have a name. Got None."
                    )

                if exog_to_check.name not in exog_names_in_:
                    if forecaster_name in ['ForecasterRecursiveMultiSeries']:
                        warnings.warn(
                            f"'{exog_to_check.name}' was not observed during training. "
                            f"{exog_name} is ignored. Exogenous variables must be one "
                            f"of: {exog_names_in_}.",
                            IgnoredArgumentWarning
                        )
                    else:
                        raise ValueError(
                            f"'{exog_to_check.name}' was not observed during training. "
                            f"Exogenous variables must be: {exog_names_in_}."
                        )

            # Check index dtype and freq
            _, exog_index = check_extract_values_and_index(
                data=exog_to_check, data_label=exog_name, ignore_freq=True, return_values=False
            )
            if not isinstance(exog_index, index_type_):
                raise TypeError(
                    f"Expected index of type {index_type_} for {exog_name}. "
                    f"Got {type(exog_index)}."
                )

            # Check exog starts one step ahead of last_window end.
            if expected_index != exog_index[0]:
                if forecaster_name in ['ForecasterRecursiveMultiSeries']:
                    warnings.warn(
                        f"To make predictions {exog_name} must start one step "
                        f"ahead of `last_window`. Missing values are filled "
                        f"with NaN.\n"
                        f"    `last_window` ends at : {last_window.index[-1]}.\n"
                        f"    {exog_name} starts at : {exog_index[0]}.\n"
                        f"    Expected index : {expected_index}.",
                        MissingValuesWarning
                    )  
                else:
                    raise ValueError(
                        f"To make predictions {exog_name} must start one step "
                        f"ahead of `last_window`.\n"
                        f"    `last_window` ends at : {last_window.index[-1]}.\n"
                        f"    {exog_name} starts at : {exog_index[0]}.\n"
                        f"    Expected index : {expected_index}."
                    )

    # Checks ForecasterStats
    if forecaster_name == 'ForecasterStats':
        # Check last_window_exog type, len, nulls and index (type and freq)
        if last_window_exog is not None:
            if not exog_in_:
                raise ValueError(
                    "Forecaster trained without exogenous variable/s. "
                    "`last_window_exog` must be `None` when predicting."
                )

            if not isinstance(last_window_exog, (pd.Series, pd.DataFrame)):
                raise TypeError(
                    f"`last_window_exog` must be a pandas Series or a "
                    f"pandas DataFrame. Got {type(last_window_exog)}."
                )
            if len(last_window_exog) < window_size:
                raise ValueError(
                    f"`last_window_exog` must have as many values as needed to "
                    f"generate the predictors. For this forecaster it is {window_size}."
                )
            if last_window_exog.isna().to_numpy().any():
                warnings.warn(
                    "`last_window_exog` has missing values. Most of machine learning "
                    "models do not allow missing values. Prediction method may fail.",
                    MissingValuesWarning
            )
            _, last_window_exog_index = check_extract_values_and_index(
                data=last_window_exog, data_label='`last_window_exog`', return_values=False
            )
            if not isinstance(last_window_exog_index, index_type_):
                raise TypeError(
                    f"Expected index of type {index_type_} for `last_window_exog`. "
                    f"Got {type(last_window_exog_index)}."
                )
            if isinstance(last_window_exog_index, pd.DatetimeIndex):
                if not last_window_exog_index.freq == index_freq_:
                    raise TypeError(
                        f"Expected frequency of type {index_freq_} for "
                        f"`last_window_exog`. Got {last_window_exog_index.freq}."
                    )

            # Check all columns are in the pd.DataFrame, last_window_exog
            if isinstance(last_window_exog, pd.DataFrame):
                col_missing = set(exog_names_in_).difference(set(last_window_exog.columns))
                if col_missing:
                    raise ValueError(
                        f"Missing columns in `last_window_exog`. Expected {exog_names_in_}. "
                        f"Got {last_window_exog.columns.to_list()}."
                    )
            else:
                if last_window_exog.name is None:
                    raise ValueError(
                        "When `last_window_exog` is a pandas Series, it must have a "
                        "name. Got None."
                    )

                if last_window_exog.name not in exog_names_in_:
                    raise ValueError(
                        f"'{last_window_exog.name}' was not observed during training. "
                        f"Exogenous variables must be: {exog_names_in_}."
                    )

skforecast.utils.utils.check_residuals_input

check_residuals_input(
    forecaster_name,
    use_in_sample_residuals,
    in_sample_residuals_,
    out_sample_residuals_,
    use_binned_residuals,
    in_sample_residuals_by_bin_,
    out_sample_residuals_by_bin_,
    levels=None,
    encoding=None,
)

Check residuals input arguments in Forecasters.

Parameters:

Name Type Description Default
forecaster_name str

Forecaster name.

required
use_in_sample_residuals bool

Indicates if in-sample or out-of-sample residuals are used.

required
in_sample_residuals_ numpy ndarray, dict

Residuals of the model when predicting training data.

required
out_sample_residuals_ numpy ndarray, dict

Residuals of the model when predicting non training data.

required
use_binned_residuals bool

Indicates if residuals are binned.

required
in_sample_residuals_by_bin_ dict

In-sample residuals binned according to the predicted value each residual is associated with.

required
out_sample_residuals_by_bin_ dict

Out of sample residuals binned according to the predicted value each residual is associated with.

required
levels list

Names of the series (levels) to be predicted (Forecasters multiseries).

None
encoding str

Encoding used to identify the different series (ForecasterRecursiveMultiSeries).

None

Returns:

Type Description
None
Source code in skforecast/utils/utils.py
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
def check_residuals_input(
    forecaster_name: str,
    use_in_sample_residuals: bool,
    in_sample_residuals_: np.ndarray | dict[str, np.ndarray] | None,
    out_sample_residuals_: np.ndarray | dict[str, np.ndarray] | None,
    use_binned_residuals: bool,
    in_sample_residuals_by_bin_: dict[str | int, np.ndarray | dict[int, np.ndarray]] | None,
    out_sample_residuals_by_bin_: dict[str | int, np.ndarray | dict[int, np.ndarray]] | None,
    levels: list[str] | None = None,
    encoding: str | None = None
) -> None:
    """
    Check residuals input arguments in Forecasters.

    Parameters
    ----------
    forecaster_name : str
        Forecaster name.
    use_in_sample_residuals : bool
        Indicates if in-sample or out-of-sample residuals are used.
    in_sample_residuals_ : numpy ndarray, dict
        Residuals of the model when predicting training data.
    out_sample_residuals_ : numpy ndarray, dict
        Residuals of the model when predicting non training data.
    use_binned_residuals : bool
        Indicates if residuals are binned.
    in_sample_residuals_by_bin_ : dict
        In-sample residuals binned according to the predicted value each residual
        is associated with.
    out_sample_residuals_by_bin_ : dict
        Out of sample residuals binned according to the predicted value each residual
        is associated with.
    levels : list, default None
        Names of the series (levels) to be predicted (Forecasters multiseries).
    encoding : str, default None
        Encoding used to identify the different series (ForecasterRecursiveMultiSeries).

    Returns
    -------
    None

    """

    forecasters_multiseries = (
        'ForecasterRecursiveMultiSeries',
        'ForecasterDirectMultiVariate',
        'ForecasterRnn'
    )

    if use_in_sample_residuals:
        if use_binned_residuals:
            residuals = in_sample_residuals_by_bin_
            literal = "in_sample_residuals_by_bin_"
        else:
            residuals = in_sample_residuals_
            literal = "in_sample_residuals_"

        # Check if residuals are empty or None
        is_empty = (
            residuals is None
            or (isinstance(residuals, dict) and not residuals)
            or (isinstance(residuals, np.ndarray) and residuals.size == 0)
        )
        if is_empty:
            raise ValueError(
                f"`forecaster.{literal}` is either None or empty. Use "
                f"`store_in_sample_residuals = True` when fitting the forecaster "
                f"or use the `set_in_sample_residuals()` method before predicting."
            )

        if forecaster_name in forecasters_multiseries:
            if encoding is not None:
                unknown_levels = set(levels) - set(residuals.keys())
                if unknown_levels:
                    warnings.warn(
                        f"`levels` {unknown_levels} are not present in `forecaster.{literal}`, "
                        f"most likely because they were not present in the training data. "
                        f"A random sample of the residuals from other levels will be used. "
                        f"This can lead to inaccurate intervals for the unknown levels.",
                        UnknownLevelWarning
                    )
    else:
        if use_binned_residuals:
            residuals = out_sample_residuals_by_bin_
            literal = "out_sample_residuals_by_bin_"
        else:
            residuals = out_sample_residuals_
            literal = "out_sample_residuals_"

        is_empty = (
            residuals is None
            or (isinstance(residuals, dict) and not residuals)
            or (isinstance(residuals, np.ndarray) and residuals.size == 0)
        )
        if is_empty:
            raise ValueError(
                f"`forecaster.{literal}` is either None or empty. Use "
                f"`use_in_sample_residuals = True` or the "
                f"`set_out_sample_residuals()` method before predicting."
            )

        if forecaster_name in forecasters_multiseries:
            if encoding is not None:
                unknown_levels = set(levels) - set(residuals.keys())
                if unknown_levels:
                    warnings.warn(
                        f"`levels` {unknown_levels} are not present in `forecaster.{literal}`. "
                        f"A random sample of the residuals from other levels will be used. "
                        f"This can lead to inaccurate intervals for the unknown levels. "
                        f"Otherwise, Use the `set_out_sample_residuals()` method before "
                        f"predicting to set the residuals for these levels.",
                        UnknownLevelWarning
                    )

    if forecaster_name in forecasters_multiseries:
        for level in residuals.keys():
            level_residuals = residuals[level]
            if level_residuals is None or len(level_residuals) == 0:
                raise ValueError(
                    f"Residuals for level '{level}' are None. Check `forecaster.{literal}`."
                )

skforecast.utils.utils.check_extract_values_and_index

check_extract_values_and_index(
    data,
    data_label="`y`",
    ignore_freq=False,
    return_values=True,
)

Return values and index of series separately. Check that index is a pandas DatetimeIndex or RangeIndex. Optionally, check that the index has a frequency.

Parameters:

Name Type Description Default
data pandas Series, pandas DataFrame

Time series.

required
data_label str

Label of the data to be used in warnings and errors.

'`y`'
ignore_freq bool

If True, ignore the frequency of the index. If False, check that the index is a pandas DatetimeIndex with a frequency.

False
return_values bool

If True return the values of data as numpy ndarray. This option is intended to avoid copying data when it is not necessary.

True

Returns:

Name Type Description
data_values numpy ndarray, None

Numpy array with values of data.

data_index pandas Index

Index of data.

Source code in skforecast/utils/utils.py
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
def check_extract_values_and_index(
    data: pd.Series | pd.DataFrame,
    data_label: str = '`y`',
    ignore_freq: bool = False,
    return_values: bool = True
) -> tuple[np.ndarray | None, pd.Index]:
    """
    Return values and index of series separately. Check that index is a pandas
    `DatetimeIndex` or `RangeIndex`. Optionally, check that the index has a
    frequency.

    Parameters
    ----------
    data : pandas Series, pandas DataFrame
        Time series.
    data_label : str, default '`y`'
        Label of the data to be used in warnings and errors.
    ignore_freq : bool, default False
        If `True`, ignore the frequency of the index. If `False`, check that the
        index is a pandas `DatetimeIndex` with a frequency.
    return_values : bool, default True
        If `True` return the values of `data` as numpy ndarray. This option is
        intended to avoid copying data when it is not necessary.

    Returns
    -------
    data_values : numpy ndarray, None
        Numpy array with values of `data`.
    data_index : pandas Index
        Index of `data`.

    """

    if isinstance(data.index, pd.DatetimeIndex):            
        if not ignore_freq and data.index.freq is None:
            raise ValueError(
                f"{data_label} has a pandas DatetimeIndex without a frequency. "
                f"To avoid this error, set the frequency of the DatetimeIndex."
            )
        data_index = data.index
    elif isinstance(data.index, pd.RangeIndex):
        data_index = data.index
    else:
        raise TypeError(
            f"{data_label} has an unsupported index type. The index must be a "
            f"pandas DatetimeIndex or a RangeIndex. Got {type(data.index)}."
        )

    data_values = data.to_numpy(copy=True).ravel() if return_values else None

    return data_values, data_index

skforecast.utils.utils.cast_exog_dtypes

cast_exog_dtypes(exog, exog_dtypes)

Cast exog to a specified types. This is done because, for a forecaster to accept a categorical exog, it must contain only integer values. Due to the internal modifications of numpy, the values may be casted to float, so they have to be re-converted to int.

  • If exog is a pandas Series, exog_dtypes must be a dict with a single value.
  • If exog_dtypes is category but the current type of exog is float, then the type is cast to int and then to category.

Parameters:

Name Type Description Default
exog pandas Series, pandas DataFrame

Exogenous variables.

required
exog_dtypes dict[str, type]

Dictionary with name and type of the series or data frame columns.

required

Returns:

Name Type Description
exog pandas Series, pandas DataFrame

Exogenous variables casted to the indicated dtypes.

Source code in skforecast/utils/utils.py
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
def cast_exog_dtypes(
    exog: pd.Series | pd.DataFrame,
    exog_dtypes: dict[str, type],
) -> pd.Series | pd.DataFrame:  # pragma: no cover
    """
    Cast `exog` to a specified types. This is done because, for a forecaster to 
    accept a categorical exog, it must contain only integer values. Due to the 
    internal modifications of numpy, the values may be casted to `float`, so 
    they have to be re-converted to `int`.

    - If `exog` is a pandas Series, `exog_dtypes` must be a dict with a 
    single value.
    - If `exog_dtypes` is `category` but the current type of `exog` is `float`, 
    then the type is cast to `int` and then to `category`. 

    Parameters
    ----------
    exog : pandas Series, pandas DataFrame
        Exogenous variables.
    exog_dtypes: dict
        Dictionary with name and type of the series or data frame columns.

    Returns
    -------
    exog : pandas Series, pandas DataFrame
        Exogenous variables casted to the indicated dtypes.

    """

    # Remove keys from exog_dtypes not in exog.columns
    exog_dtypes = {k: v for k, v in exog_dtypes.items() if k in exog.columns}

    if isinstance(exog, pd.Series) and exog.dtypes != list(exog_dtypes.values())[0]:
        exog = exog.astype(list(exog_dtypes.values())[0])
    elif isinstance(exog, pd.DataFrame):
        for col, initial_dtype in exog_dtypes.items():
            if exog[col].dtypes != initial_dtype:
                if initial_dtype == "category" and exog[col].dtypes == float:
                    exog[col] = exog[col].astype(int).astype("category")
                else:
                    exog[col] = exog[col].astype(initial_dtype)

    return exog

skforecast.utils.utils.exog_to_direct

exog_to_direct(exog, steps)

Transforms exog to a pandas DataFrame with the shape needed for Direct forecasting.

Parameters:

Name Type Description Default
exog pandas Series, pandas DataFrame

Exogenous variables.

required
steps int

Number of steps that will be predicted using exog.

required

Returns:

Name Type Description
exog_direct pandas DataFrame

Exogenous variables transformed.

exog_direct_names list

Names of the columns of the exogenous variables transformed. Only created if exog is a pandas Series or DataFrame.

Source code in skforecast/utils/utils.py
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
def exog_to_direct(
    exog: pd.Series | pd.DataFrame,
    steps: int
) -> tuple[pd.DataFrame, list[str]]:
    """
    Transforms `exog` to a pandas DataFrame with the shape needed for Direct
    forecasting.

    Parameters
    ----------
    exog : pandas Series, pandas DataFrame
        Exogenous variables.
    steps : int
        Number of steps that will be predicted using exog.

    Returns
    -------
    exog_direct : pandas DataFrame
        Exogenous variables transformed.
    exog_direct_names : list
        Names of the columns of the exogenous variables transformed. Only 
        created if `exog` is a pandas Series or DataFrame.

    """

    if not isinstance(exog, (pd.Series, pd.DataFrame)):
        raise TypeError(f"`exog` must be a pandas Series or DataFrame. Got {type(exog)}.")

    if isinstance(exog, pd.Series):
        exog = exog.to_frame()

    n_rows = len(exog)
    exog_idx = exog.index
    exog_cols = exog.columns
    exog_direct = []
    for i in range(steps):
        exog_step = exog.iloc[i : n_rows - (steps - 1 - i), ]
        exog_step.index = pd.RangeIndex(len(exog_step))
        exog_step.columns = [f"{col}_step_{i + 1}" for col in exog_cols]
        exog_direct.append(exog_step)

    exog_direct = pd.concat(exog_direct, axis=1) if steps > 1 else exog_direct[0]

    exog_direct_names = exog_direct.columns.to_list()
    exog_direct.index = exog_idx[-len(exog_direct):]

    return exog_direct, exog_direct_names

skforecast.utils.utils.exog_to_direct_numpy

exog_to_direct_numpy(exog, steps)

Transforms exog to numpy ndarray with the shape needed for Direct forecasting.

Parameters:

Name Type Description Default
exog numpy ndarray, pandas Series, pandas DataFrame

Exogenous variables, shape(samples,). If exog is a pandas format, the direct exog names are created.

required
steps int

Number of steps that will be predicted using exog.

required

Returns:

Name Type Description
exog_direct numpy ndarray

Exogenous variables transformed.

exog_direct_names (list, None)

Names of the columns of the exogenous variables transformed. Only created if exog is a pandas Series or DataFrame.

Source code in skforecast/utils/utils.py
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
def exog_to_direct_numpy(
    exog: np.ndarray | pd.Series | pd.DataFrame,
    steps: int
) -> tuple[np.ndarray, list[str] | None]:
    """
    Transforms `exog` to numpy ndarray with the shape needed for Direct
    forecasting.

    Parameters
    ----------
    exog : numpy ndarray, pandas Series, pandas DataFrame
        Exogenous variables, shape(samples,). If exog is a pandas format, the 
        direct exog names are created.
    steps : int
        Number of steps that will be predicted using exog.

    Returns
    -------
    exog_direct : numpy ndarray
        Exogenous variables transformed.
    exog_direct_names : list, None
        Names of the columns of the exogenous variables transformed. Only 
        created if `exog` is a pandas Series or DataFrame.

    """

    if isinstance(exog, (pd.Series, pd.DataFrame)):
        exog_cols = exog.columns if isinstance(exog, pd.DataFrame) else [exog.name]
        exog_direct_names = [
            f"{col}_step_{i + 1}" for i in range(steps) for col in exog_cols
        ]
        exog = exog.to_numpy()
    else:
        exog_direct_names = None
        if not isinstance(exog, np.ndarray):
            raise TypeError(
                f"`exog` must be a numpy ndarray, pandas Series or DataFrame. "
                f"Got {type(exog)}."
            )

    if exog.ndim == 1:
        exog = np.expand_dims(exog, axis=1)

    n_rows = len(exog)
    exog_direct = [exog[i : n_rows - (steps - 1 - i)] for i in range(steps)]
    exog_direct = np.concatenate(exog_direct, axis=1) if steps > 1 else exog_direct[0]

    return exog_direct, exog_direct_names

skforecast.utils.utils.expand_index

expand_index(index, steps)

Create a new index of length steps starting at the end of the index.

Parameters:

Name Type Description Default
index pandas Index, None

Original index.

required
steps int

Number of steps to expand.

required

Returns:

Name Type Description
new_index pandas Index

New index.

Source code in skforecast/utils/utils.py
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
def expand_index(
    index: pd.Index | None, 
    steps: int
) -> pd.Index:
    """
    Create a new index of length `steps` starting at the end of the index.

    Parameters
    ----------
    index : pandas Index, None
        Original index.
    steps : int
        Number of steps to expand.

    Returns
    -------
    new_index : pandas Index
        New index.

    """

    if not isinstance(steps, (int, np.integer)):
        raise TypeError(f"`steps` must be an integer. Got {type(steps)}.")

    if isinstance(index, pd.Index):

        if isinstance(index, pd.DatetimeIndex):
            new_index = pd.date_range(
                            start   = index[-1] + index.freq,
                            periods = steps,
                            freq    = index.freq
                        )
        elif isinstance(index, pd.RangeIndex):
            new_index = pd.RangeIndex(
                            start = index[-1] + index.step,
                            stop  = index[-1] + index.step + steps * index.step,
                            step  = index.step
                        )
        else:
            raise TypeError(
                "Argument `index` must be a pandas DatetimeIndex or RangeIndex."
            )
    else:
        new_index = pd.RangeIndex(
                        start = 0,
                        stop  = steps
                    )

    return new_index

skforecast.utils.utils.transform_numpy

transform_numpy(
    array,
    transformer,
    fit=False,
    inverse_transform=False,
    force_single_column=False,
)

Transform raw values of a numpy ndarray with a scikit-learn alike transformer, preprocessor or ColumnTransformer. The transformer used must have the following methods: fit, transform, fit_transform and inverse_transform. ColumnTransformers are not allowed since they do not have inverse_transform method.

Parameters:

Name Type Description Default
array numpy ndarray

Array to be transformed.

required
transformer scikit-learn alike transformer, preprocessor, or ColumnTransformer.

Scikit-learn alike transformer (preprocessor) with methods: fit, transform, fit_transform and inverse_transform.

required
fit bool

Train the transformer before applying it.

False
inverse_transform bool

Transform back the data to the original representation. This is not available when using transformers of class scikit-learn ColumnTransformers.

False
force_single_column bool

If True, raise an error if the transformer generates more than one column. This ensures that the output array is always 1D or single-column.

False

Returns:

Name Type Description
array_transformed numpy ndarray

Transformed array.

Source code in skforecast/utils/utils.py
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
def transform_numpy(
    array: np.ndarray,
    transformer: object | None,
    fit: bool = False,
    inverse_transform: bool = False,
    force_single_column: bool = False
) -> np.ndarray:
    """
    Transform raw values of a numpy ndarray with a scikit-learn alike 
    transformer, preprocessor or ColumnTransformer. The transformer used must 
    have the following methods: fit, transform, fit_transform and 
    inverse_transform. ColumnTransformers are not allowed since they do not 
    have inverse_transform method.

    Parameters
    ----------
    array : numpy ndarray
        Array to be transformed.
    transformer : scikit-learn alike transformer, preprocessor, or ColumnTransformer.
        Scikit-learn alike transformer (preprocessor) with methods: fit, transform,
        fit_transform and inverse_transform.
    fit : bool, default False
        Train the transformer before applying it.
    inverse_transform : bool, default False
        Transform back the data to the original representation. This is not available
        when using transformers of class scikit-learn ColumnTransformers.
    force_single_column : bool, default False
        If `True`, raise an error if the transformer generates more than one
        column. This ensures that the output array is always 1D or single-column.

    Returns
    -------
    array_transformed : numpy ndarray
        Transformed array.

    """

    if transformer is None:
        return array

    if not isinstance(array, np.ndarray):
        raise TypeError(
            f"`array` argument must be a numpy ndarray. Got {type(array)}"
        )

    original_ndim = array.ndim
    original_shape = array.shape
    reshaped_for_inverse = False

    if original_ndim == 1:
        array = array.reshape(-1, 1)

    if inverse_transform and isinstance(transformer, ColumnTransformer):
        raise ValueError(
            "`inverse_transform` is not available when using ColumnTransformers."
        )

    with warnings.catch_warnings():
        warnings.filterwarnings(
            "ignore", 
            message="X does not have valid feature names", 
            category=UserWarning
        )
        if inverse_transform:
            # Vectorized inverse transformation for 2D arrays with multiple columns.
            # Reshape to single column, transform, and reshape back.
            # This is faster than applying the transformer column by column.
            if array.shape[1] > 1:
                array = array.reshape(-1, 1)
                reshaped_for_inverse = True
            array_transformed = transformer.inverse_transform(array)
        elif fit:
            array_transformed = transformer.fit_transform(array)
        else:
            array_transformed = transformer.transform(array)

    if hasattr(array_transformed, 'toarray'):
        # If the returned values are in sparse matrix format, it is converted to dense
        array_transformed = array_transformed.toarray()

    if isinstance(array_transformed, (pd.Series, pd.DataFrame)):
        array_transformed = array_transformed.to_numpy()

    if force_single_column and array_transformed.ndim > 1 and array_transformed.shape[1] > 1:
        raise ValueError(
            f"`transformer_y` and `transformer_series` must return a single column. "
            f"The transformer generated {array_transformed.shape[1]} columns. "
            f"Transformers that expand target series into multiple feature "
            f"columns are not supported; use `window_features` or pass "
            f"those features through `exog` instead."
        )

    # Reshape back to original shape only if we reshaped for inverse_transform
    if reshaped_for_inverse:
        array_transformed = array_transformed.reshape(original_shape)

    if original_ndim == 1:
        array_transformed = array_transformed.ravel()

    return array_transformed

skforecast.utils.utils.transform_series

transform_series(
    series,
    transformer,
    fit=False,
    inverse_transform=False,
    force_single_column=False,
)

Transform raw values of pandas Series with a scikit-learn alike transformer, preprocessor or ColumnTransformer. The transformer used must have the following methods: fit, transform, fit_transform and inverse_transform. ColumnTransformers are not allowed since they do not have inverse_transform method.

Parameters:

Name Type Description Default
series pandas Series

Series to be transformed.

required
transformer scikit-learn alike transformer, preprocessor, or ColumnTransformer.

Scikit-learn alike transformer (preprocessor) with methods: fit, transform, fit_transform and inverse_transform.

required
fit bool

Train the transformer before applying it.

False
inverse_transform bool

Transform back the data to the original representation. This is not available when using transformers of class scikit-learn ColumnTransformers.

False
force_single_column bool

If True, raise an error if the transformer generates more than one column. This ensures that the output is always a pandas Series.

False

Returns:

Name Type Description
series_transformed pandas Series, pandas DataFrame

Transformed Series. Depending on the transformer used, the output may be a Series or a DataFrame.

Source code in skforecast/utils/utils.py
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
def transform_series(
    series: pd.Series,
    transformer: object | None,
    fit: bool = False,
    inverse_transform: bool = False,
    force_single_column: bool = False
) -> pd.Series | pd.DataFrame:
    """
    Transform raw values of pandas Series with a scikit-learn alike 
    transformer, preprocessor or ColumnTransformer. The transformer used must 
    have the following methods: fit, transform, fit_transform and 
    inverse_transform. ColumnTransformers are not allowed since they do not 
    have inverse_transform method.

    Parameters
    ----------
    series : pandas Series
        Series to be transformed.
    transformer : scikit-learn alike transformer, preprocessor, or ColumnTransformer.
        Scikit-learn alike transformer (preprocessor) with methods: fit, transform,
        fit_transform and inverse_transform.
    fit : bool, default False
        Train the transformer before applying it.
    inverse_transform : bool, default False
        Transform back the data to the original representation. This is not available
        when using transformers of class scikit-learn ColumnTransformers.
    force_single_column : bool, default False
        If `True`, raise an error if the transformer generates more than one
        column. This ensures that the output is always a pandas Series.

    Returns
    -------
    series_transformed : pandas Series, pandas DataFrame
        Transformed Series. Depending on the transformer used, the output may 
        be a Series or a DataFrame.

    """

    if not isinstance(series, pd.Series):
        raise TypeError(
            f"`series` argument must be a pandas Series. Got {type(series)}."
        )

    if transformer is None:
        return series

    series_name = series.name if series.name is not None else 'no_name'
    data = series.to_frame(name=series_name)

    # If argument feature_names_in_ exits, is overwritten to allow using the 
    # transformer on other series than those that were passed during fit.
    if not fit and hasattr(transformer, 'feature_names_in_') and transformer.feature_names_in_[0] != data.columns[0]:
        transformer = deepcopy(transformer)
        transformer.feature_names_in_ = np.array([data.columns[0]], dtype=object)

    with warnings.catch_warnings():
        warnings.simplefilter("ignore", category=UserWarning)
        if inverse_transform:
            values_transformed = transformer.inverse_transform(data)
        elif fit:
            values_transformed = transformer.fit_transform(data)
        else:
            values_transformed = transformer.transform(data)   

    if hasattr(values_transformed, 'toarray'):
        # If the returned values are in sparse matrix format, it is converted to dense array.
        values_transformed = values_transformed.toarray()

    if isinstance(values_transformed, np.ndarray) and values_transformed.shape[1] == 1:
        series_transformed = pd.Series(
                                 data  = values_transformed.ravel(),
                                 index = data.index,
                                 name  = data.columns[0]
                             )
    elif isinstance(values_transformed, pd.DataFrame) and values_transformed.shape[1] == 1:
        series_transformed = values_transformed.squeeze()
    else:
        if force_single_column:
            raise ValueError(
                f"`transformer_y` and `transformer_series` must return a single column. "
                f"The transformer generated {values_transformed.shape[1]} columns. "
                f"Transformers that expand target series into multiple feature "
                f"columns are not supported; use `window_features` or pass "
                f"those features through `exog` instead."
            )
        if hasattr(transformer, 'get_feature_names_out'):
            feature_names_out = transformer.get_feature_names_out()
            if len(feature_names_out) != values_transformed.shape[1]:
                feature_names_out = [f'transformed_{i}' for i in range(values_transformed.shape[1])]
        else:
            feature_names_out = [f'transformed_{i}' for i in range(values_transformed.shape[1])]

        series_transformed = pd.DataFrame(
                                 data    = values_transformed,
                                 index   = data.index,
                                 columns = feature_names_out
                             )

    return series_transformed

skforecast.utils.utils.transform_dataframe

transform_dataframe(
    df,
    transformer,
    fit=False,
    inverse_transform=False,
    force_single_column=False,
)

Transform raw values of pandas DataFrame with a scikit-learn alike transformer, preprocessor or ColumnTransformer. The transformer used must have the following methods: fit, transform, fit_transform and inverse_transform. ColumnTransformers are not allowed since they do not have inverse_transform method.

Parameters:

Name Type Description Default
df pandas DataFrame

DataFrame to be transformed.

required
transformer scikit-learn alike transformer, preprocessor, or ColumnTransformer.

Scikit-learn alike transformer (preprocessor) with methods: fit, transform, fit_transform and inverse_transform.

required
fit bool

Train the transformer before applying it.

False
inverse_transform bool

Transform back the data to the original representation. This is not available when using transformers of class scikit-learn ColumnTransformers.

False
force_single_column bool

If True, raise an error if the transformer generates more than one column. This ensures that the output DataFrame has a single column.

False

Returns:

Name Type Description
df_transformed pandas DataFrame

Transformed DataFrame.

Source code in skforecast/utils/utils.py
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
def transform_dataframe(
    df: pd.DataFrame,
    transformer: object | None,
    fit: bool = False,
    inverse_transform: bool = False,
    force_single_column: bool = False
) -> pd.DataFrame:
    """
    Transform raw values of pandas DataFrame with a scikit-learn alike 
    transformer, preprocessor or ColumnTransformer. The transformer used must 
    have the following methods: fit, transform, fit_transform and 
    inverse_transform. ColumnTransformers are not allowed since they do not 
    have inverse_transform method.

    Parameters
    ----------
    df : pandas DataFrame
        DataFrame to be transformed.
    transformer : scikit-learn alike transformer, preprocessor, or ColumnTransformer.
        Scikit-learn alike transformer (preprocessor) with methods: fit, transform,
        fit_transform and inverse_transform.
    fit : bool, default False
        Train the transformer before applying it.
    inverse_transform : bool, default False
        Transform back the data to the original representation. This is not available
        when using transformers of class scikit-learn ColumnTransformers.
    force_single_column : bool, default False
        If `True`, raise an error if the transformer generates more than one
        column. This ensures that the output DataFrame has a single column.

    Returns
    -------
    df_transformed : pandas DataFrame
        Transformed DataFrame.

    """

    if not isinstance(df, pd.DataFrame):
        raise TypeError(
            f"`df` argument must be a pandas DataFrame. Got {type(df)}"
        )

    if transformer is None:
        return df

    if inverse_transform and isinstance(transformer, ColumnTransformer):
        raise ValueError(
            "`inverse_transform` is not available when using ColumnTransformers."
        )

    if inverse_transform:
        values_transformed = transformer.inverse_transform(df)
    elif fit:
        values_transformed = transformer.fit_transform(df)
    else:
        values_transformed = transformer.transform(df)

    if hasattr(values_transformed, 'toarray'):
        # If the returned values are in sparse matrix format, it is converted to dense
        values_transformed = values_transformed.toarray()

    if isinstance(values_transformed, pd.DataFrame):
        df_transformed = values_transformed
    else:
        values_transformed = np.asarray(values_transformed)
        if values_transformed.ndim == 1:
            values_transformed = values_transformed.reshape(-1, 1)

        feature_names_out = (
            transformer.get_feature_names_out()
            if hasattr(transformer, 'get_feature_names_out')
            else df.columns
        )
        if len(feature_names_out) != values_transformed.shape[1]:
            feature_names_out = [f'transformed_{i}' for i in range(values_transformed.shape[1])]

        df_transformed = pd.DataFrame(
                             data    = values_transformed,
                             index   = df.index,
                             columns = feature_names_out
                         )

    if force_single_column and df_transformed.shape[1] > 1:
        raise ValueError(
            f"`transformer_y` and `transformer_series` must return a single column. "
            f"The transformer generated {df_transformed.shape[1]} columns. "
            f"Transformers that expand target series into multiple feature "
            f"columns are not supported; use `window_features` or pass "
            f"those features through `exog` instead."
        )

    return df_transformed

skforecast.utils.utils.check_optional_dependency

check_optional_dependency(package_name)

Check if an optional dependency is installed, if not raise an ImportError
with installation instructions.

Parameters:

Name Type Description Default
package_name str

Name of the package to check.

required

Returns:

Type Description
None
Source code in skforecast/utils/utils.py
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
def check_optional_dependency(
    package_name: str
) -> None:
    """
    Check if an optional dependency is installed, if not raise an ImportError  
    with installation instructions.

    Parameters
    ----------
    package_name : str
        Name of the package to check.

    Returns
    -------
    None

    """

    if find_spec(package_name) is None:
        try:
            extra, package_version = _find_optional_dependency(package_name=package_name)
            msg = (
                f"\n'{package_name}' is an optional dependency not included in the default "
                f"skforecast installation. Please run: `pip install \"{package_version}\"` to install it."
                f"\n\nAlternately, you can install it by running `pip install skforecast[{extra}]`"
            )
        except ValueError:
            msg = f"\n'{package_name}' is needed but not installed. Please install it."

        raise ImportError(msg)

skforecast.utils.utils.multivariate_time_series_corr

multivariate_time_series_corr(
    time_series, other, lags, method="pearson"
)

Compute correlation between a time_series and the lagged values of other time series.

Parameters:

Name Type Description Default
time_series pandas Series

Target time series.

required
other pandas DataFrame

Time series whose lagged values are correlated to time_series.

required
lags int, list, numpy ndarray

Lags to be included in the correlation analysis.

required
method str
  • 'pearson': standard correlation coefficient.
  • 'kendall': Kendall Tau correlation coefficient.
  • 'spearman': Spearman rank correlation.
'pearson'

Returns:

Name Type Description
corr pandas DataFrame

Correlation values.

Source code in skforecast/utils/utils.py
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
def multivariate_time_series_corr(
    time_series: pd.Series,
    other: pd.DataFrame,
    lags: int | list[int] | np.ndarray[int],
    method: str = 'pearson'
) -> pd.DataFrame:
    """
    Compute correlation between a time_series and the lagged values of other 
    time series. 

    Parameters
    ----------
    time_series : pandas Series
        Target time series.
    other : pandas DataFrame
        Time series whose lagged values are correlated to `time_series`.
    lags : int, list, numpy ndarray
        Lags to be included in the correlation analysis.
    method : str, default 'pearson'
        - 'pearson': standard correlation coefficient.
        - 'kendall': Kendall Tau correlation coefficient.
        - 'spearman': Spearman rank correlation.

    Returns
    -------
    corr : pandas DataFrame
        Correlation values.

    """

    if not len(time_series) == len(other):
        raise ValueError("`time_series` and `other` must have the same length.")

    if not (time_series.index == other.index).all():
        raise ValueError("`time_series` and `other` must have the same index.")

    if isinstance(lags, int):
        lags = range(lags)

    corr = {}
    for col in other.columns:
        lag_values = {}
        for lag in lags:
            lag_values[lag] = other[col].shift(lag)

        lag_values = pd.DataFrame(lag_values)
        lag_values.insert(0, None, time_series)
        corr[col] = lag_values.corr(method=method).iloc[1:, 0]

    corr = pd.DataFrame(corr)
    corr.index = corr.index.astype('int64')
    corr.index.name = "lag"

    return corr

skforecast.utils.utils.select_n_jobs_fit_forecaster

select_n_jobs_fit_forecaster(forecaster_name, estimator)

Select the optimal number of jobs to use in the fitting process. This selection is based on heuristics and is not guaranteed to be optimal.

The number of jobs is chosen as follows:

  • If forecaster_name is 'ForecasterDirect' or 'ForecasterDirectMultiVariate' and estimator_name is a linear estimator then n_jobs = 1, otherwise n_jobs = max(1, cpu_count() - 1).
  • If estimator is a LGBMRegressor(n_jobs=1), then n_jobs = max(1, cpu_count() - 1).
  • If estimator is a LGBMRegressor with internal n_jobs != 1, then n_jobs = 1. This is because lightgbm is highly optimized for gradient boosting and parallelizes operations at a very fine-grained level, making additional parallelization unnecessary and potentially harmful due to resource contention.

Parameters:

Name Type Description Default
forecaster_name str

Forecaster name.

required
estimator estimator or pipeline compatible with the scikit-learn API

An instance of an estimator or pipeline compatible with the scikit-learn API.

required

Returns:

Name Type Description
n_jobs int

The number of jobs to run in parallel.

Source code in skforecast/utils/utils.py
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
def select_n_jobs_fit_forecaster(
    forecaster_name: str,
    estimator: object
) -> int:
    """
    Select the optimal number of jobs to use in the fitting process. This
    selection is based on heuristics and is not guaranteed to be optimal. 

    The number of jobs is chosen as follows:

    - If forecaster_name is 'ForecasterDirect' or 'ForecasterDirectMultiVariate'
    and estimator_name is a linear estimator then `n_jobs = 1`, 
    otherwise `n_jobs = max(1, cpu_count() - 1)`.
    - If estimator is a `LGBMRegressor(n_jobs=1)`, then `n_jobs = max(1, cpu_count() - 1)`.
    - If estimator is a `LGBMRegressor` with internal n_jobs != 1, then `n_jobs = 1`.
    This is because `lightgbm` is highly optimized for gradient boosting and
    parallelizes operations at a very fine-grained level, making additional
    parallelization unnecessary and potentially harmful due to resource contention.

    Parameters
    ----------
    forecaster_name : str
        Forecaster name.
    estimator : estimator or pipeline compatible with the scikit-learn API
        An instance of an estimator or pipeline compatible with the scikit-learn API.

    Returns
    -------
    n_jobs : int
        The number of jobs to run in parallel.

    """

    if isinstance(estimator, Pipeline):
        estimator = estimator[-1]

    if forecaster_name in {'ForecasterDirect', 'ForecasterDirectMultiVariate'}:
        if isinstance(estimator, LinearModel):
            n_jobs = 1
        elif type(estimator).__name__ == 'LGBMRegressor':
            n_jobs = max(1, joblib.cpu_count() - 1) if estimator.n_jobs == 1 else 1
        else:
            n_jobs = max(1, joblib.cpu_count() - 1)
    else:
        n_jobs = 1

    return n_jobs

skforecast.utils.utils.check_preprocess_series

check_preprocess_series(series)

Check and preprocess series argument in ForecasterRecursiveMultiSeries class.

  • If series is a wide-format pandas DataFrame, each column represents a different time series, and the index must be either a DatetimeIndex or a RangeIndex with frequency or step size, as appropriate
  • If series is a long-format pandas DataFrame with a MultiIndex, the first level of the index must contain the series IDs, and the second level must be a DatetimeIndex with the same frequency across all series.
  • If series is a dictionary, each key must be a series ID, and each value must be a named pandas Series. All series must have the same index, which must be either a DatetimeIndex or a RangeIndex, and they must share the same frequency or step size, as appropriate.

When series is a pandas DataFrame, it is converted to a dictionary of pandas Series, where the keys are the series IDs and the values are the Series with the same index as the original DataFrame.

Parameters:

Name Type Description Default
series pandas DataFrame, dict

Training time series.

required

Returns:

Name Type Description
series_dict dict

Dictionary with the series used during training.

series_indexes dict

Dictionary with the index of each series.

Source code in skforecast/utils/utils.py
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
def check_preprocess_series(
    series: pd.DataFrame | dict[str, pd.Series | pd.DataFrame],
) -> tuple[dict[str, pd.Series], dict[str, pd.Index]]:
    """
    Check and preprocess `series` argument in `ForecasterRecursiveMultiSeries` class.

    - If `series` is a wide-format pandas DataFrame, each column represents a
    different time series, and the index must be either a `DatetimeIndex` or 
    a `RangeIndex` with frequency or step size, as appropriate
    - If `series` is a long-format pandas DataFrame with a MultiIndex, the 
    first level of the index must contain the series IDs, and the second 
    level must be a `DatetimeIndex` with the same frequency across all series.
    - If series is a dictionary, each key must be a series ID, and each value 
    must be a named pandas Series. All series must have the same index, which 
    must be either a `DatetimeIndex` or a `RangeIndex`, and they must share the 
    same frequency or step size, as appropriate.

    When `series` is a pandas DataFrame, it is converted to a dictionary of pandas 
    Series, where the keys are the series IDs and the values are the Series with 
    the same index as the original DataFrame.

    Parameters
    ----------
    series : pandas DataFrame, dict
        Training time series.

    Returns
    -------
    series_dict : dict
        Dictionary with the series used during training.
    series_indexes : dict
        Dictionary with the index of each series.

    """

    if not isinstance(series, (pd.DataFrame, dict)):
        raise TypeError(
            f"`series` must be a pandas DataFrame or a dict of DataFrames or Series. "
            f"Got {type(series)}."
        )

    if isinstance(series, pd.DataFrame):

        if not isinstance(series.index, pd.MultiIndex):
            _, _ = check_extract_values_and_index(
                data=series, data_label='`series`', return_values=False
            )
            series = series.copy()
            series.index.name = None
            series_dict = series.to_dict(orient='series')
        else:
            if not isinstance(series.index.levels[1], pd.DatetimeIndex):
                raise TypeError(
                    f"The second level of the MultiIndex in `series` must be a "
                    f"pandas DatetimeIndex with the same frequency for each series. "
                    f"Found {type(series.index.levels[1])}."
                )

            first_col = series.columns[0]
            if len(series.columns) != 1:
                warnings.warn(
                    f"`series` DataFrame has multiple columns. Only the values of "
                    f"first column, '{first_col}', will be used as series values. "
                    f"All other columns will be ignored.",
                    IgnoredArgumentWarning
                )

            series = series.copy()
            series.index = series.index.set_names([series.index.names[0], None])
            series_dict = {
                series_id: group[first_col].droplevel(0).rename(series_id)
                for series_id, group in series.groupby(level=0, sort=True, observed=True)
            }

        warnings.warn(
            "Passing a DataFrame (either wide or long format) as `series` requires "
            "additional internal transformations, which can increase computational "
            "time. It is recommended to use a dictionary of pandas Series instead. "
            "For more details, see: "
            "https://skforecast.org/latest/user_guides/independent-multi-time-series-forecasting.html#input-data",
            InputTypeWarning
        )

    else:

        not_valid_series = [
            k 
            for k, v in series.items()
            if not isinstance(v, (pd.Series, pd.DataFrame))
        ]
        if not_valid_series:
            raise TypeError(
                f"If `series` is a dictionary, all series must be a named "
                f"pandas Series or a pandas DataFrame with a single column. "
                f"Review series: {not_valid_series}"
            )

        series_dict = {
            k: v.copy()
            for k, v in series.items()
        }

    not_valid_index = []
    indexes_freq = set()
    series_indexes = {}
    for k, v in series_dict.items():
        if isinstance(v, pd.DataFrame):
            if v.shape[1] != 1:
                raise ValueError(
                    f"If `series` is a dictionary, all series must be a named "
                    f"pandas Series or a pandas DataFrame with a single column. "
                    f"Review series: '{k}'"
                )
            series_dict[k] = v.iloc[:, 0]

        series_dict[k].name = k
        idx = v.index
        if isinstance(idx, pd.DatetimeIndex):
            indexes_freq.add(idx.freq)
        elif isinstance(idx, pd.RangeIndex):
            indexes_freq.add(idx.step)
        else:
            not_valid_index.append(k)

        if v.isna().to_numpy().all():
            raise ValueError(f"All values of series '{k}' are NaN.")

        series_indexes[k] = idx

    if not_valid_index:
        raise TypeError(
            f"If `series` is a dictionary, all series must have a Pandas "
            f"RangeIndex or DatetimeIndex with the same step/frequency. "
            f"Review series: {not_valid_index}"
        )
    if None in indexes_freq:
        raise ValueError(
            "If `series` is a dictionary, all series must have a Pandas "
            "RangeIndex or DatetimeIndex with the same step/frequency. "
            "If it a MultiIndex DataFrame, the second level must be a DatetimeIndex "
            "with the same frequency for each series. Found series with no "
            "frequency or step."
        )
    if not len(indexes_freq) == 1:
        raise ValueError(
            f"If `series` is a dictionary, all series must have a Pandas "
            f"RangeIndex or DatetimeIndex with the same step/frequency. "
            f"If it a MultiIndex DataFrame, the second level must be a DatetimeIndex "
            f"with the same frequency for each series. "
            f"Found frequencies: {sorted(indexes_freq)}"
        )

    return series_dict, series_indexes

skforecast.utils.utils.check_preprocess_exog_multiseries

check_preprocess_exog_multiseries(
    series_names_in_, series_index_type, exog, exog_dict
)

Check and preprocess exog argument in ForecasterRecursiveMultiSeries class.

  • If exog is a wide-format pandas DataFrame, it must share the same index type as series. Each column represents a different exogenous variable, and the same values are applied to all time series.
  • If exog is a long-format pandas Series or DataFrame with a MultiIndex, the first level contains the series IDs to which it belongs, and the second level contains a pandas DatetimeIndex. One column must be created for each exogenous variable.
  • If exog is a dictionary, each key must be the series ID to which it belongs, and each value must be a named pandas Series/DataFrame with the same index type as series or None. While it is not necessary for all values to include all the exogenous variables, the dtypes must be consistent for the same exogenous variable across all series.

When exog is a pandas DataFrame, it is converted to a dictionary of pandas DataFrames, where the keys are the series IDs and the values are the Series with the same index as the original DataFrame.

Parameters:

Name Type Description Default
series_names_in_ list

Names of the series (levels) used during training.

required
series_index_type type

Index type of the series used during training.

required
exog pandas Series, pandas DataFrame, dict

Exogenous variable/s used during training.

required
exog_dict dict

Dictionary with the exogenous variable/s used during training.

required

Returns:

Name Type Description
exog_dict dict

Dictionary with the exogenous variable/s used during training.

exog_names_in_ list

Names of the exogenous variables used during training.

Source code in skforecast/utils/utils.py
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
3573
3574
3575
3576
3577
3578
3579
3580
3581
3582
3583
3584
3585
3586
3587
3588
3589
3590
3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602
3603
3604
3605
3606
3607
3608
3609
3610
3611
3612
3613
3614
3615
3616
3617
3618
3619
3620
3621
3622
3623
3624
3625
3626
3627
3628
3629
3630
3631
3632
3633
3634
3635
3636
3637
3638
3639
3640
3641
3642
3643
3644
3645
3646
def check_preprocess_exog_multiseries(
    series_names_in_: list[str],
    series_index_type: type,
    exog: pd.Series | pd.DataFrame | dict[str, pd.Series | pd.DataFrame | None],
    exog_dict: dict[str, pd.Series | pd.DataFrame | None],
) -> tuple[dict[str, pd.DataFrame | None], list[str]]:
    """
    Check and preprocess `exog` argument in `ForecasterRecursiveMultiSeries` class.

    - If `exog` is a wide-format pandas DataFrame, it must share the same 
    index type as series. Each column represents a different exogenous variable, 
    and the same values are applied to all time series.
    - If `exog` is a long-format pandas Series or DataFrame with a MultiIndex, 
    the first level contains the series IDs to which it belongs, and the 
    second level contains a pandas DatetimeIndex. One column must be created
    for each exogenous variable.
    - If `exog` is a dictionary, each key must be the series ID to which it 
    belongs, and each value must be a named pandas Series/DataFrame with
    the same index type as `series` or None. While it is not necessary for 
    all values to include all the exogenous variables, the dtypes must be 
    consistent for the same exogenous variable across all series.

    When `exog` is a pandas DataFrame, it is converted to a dictionary of pandas 
    DataFrames, where the keys are the series IDs and the values are the Series 
    with the same index as the original DataFrame.

    Parameters
    ----------
    series_names_in_ : list
        Names of the series (levels) used during training.
    series_index_type : type
        Index type of the series used during training.
    exog : pandas Series, pandas DataFrame, dict
        Exogenous variable/s used during training.
    exog_dict : dict
        Dictionary with the exogenous variable/s used during training.

    Returns
    -------
    exog_dict : dict
        Dictionary with the exogenous variable/s used during training.
    exog_names_in_ : list
        Names of the exogenous variables used during training.

    """

    if not isinstance(exog, (pd.Series, pd.DataFrame, dict)):
        raise TypeError(
            f"`exog` must be a pandas Series, DataFrame, dictionary of pandas "
            f"Series/DataFrames or None. Got {type(exog)}."
        )

    if isinstance(exog, (pd.Series, pd.DataFrame)): 

        exog = exog.copy().to_frame() if isinstance(exog, pd.Series) else exog.copy()
        if isinstance(exog.index, pd.MultiIndex):
            if not isinstance(exog.index.levels[1], pd.DatetimeIndex):
                raise TypeError(
                    f"When input data are pandas MultiIndex DataFrame, "
                    f"`series` and `exog` second level index must be a "
                    f"pandas DatetimeIndex. Found `exog` index type: "
                    f"{type(exog.index.levels[1])}."
                )
            exog.index = exog.index.set_names([exog.index.names[0], None])
            exog_dict.update(
                {
                    series_id: group.droplevel(0)
                    for series_id, group in exog.groupby(level=0, sort=True, observed=True)
                    if series_id in series_names_in_
                }
            )
            series_ids_in_exog = exog.index.remove_unused_levels().levels[0]
            warnings.warn(
                "Using a long-format DataFrame as `exog` requires additional transformations, "
                "which can increase computational time. It is recommended to use a dictionary of "
                "Series or DataFrames instead. For more information, see: "
                "https://skforecast.org/latest/user_guides/independent-multi-time-series-forecasting#input-data",
                InputTypeWarning
            )
        else:
            if not isinstance(exog.index, series_index_type):
                raise TypeError(
                    f"`exog` must have the same index type as `series`, pandas "
                    f"RangeIndex or pandas DatetimeIndex.\n"
                    f"    `series` index type : {series_index_type}.\n"
                    f"    `exog`   index type : {type(exog.index)}."
                )
            exog_dict = {series_id: exog for series_id in series_names_in_}
            series_ids_in_exog = series_names_in_

    else:

        not_valid_exog = [
            k 
            for k, v in exog.items()
            if not isinstance(v, (pd.Series, pd.DataFrame, type(None)))
        ]
        if not_valid_exog:
            raise TypeError(
                f"If `exog` is a dictionary, all exog must be a named pandas "
                f"Series, a pandas DataFrame or None. Review exog: {not_valid_exog}"
            )

        # NOTE: Only elements already present in exog_dict are updated. Copy is
        # needed to avoid modifying the original exog.
        exog_dict.update(
            {
                k: v.copy()
                for k, v in exog.items()
                if k in series_names_in_ and v is not None
            }
        )
        series_ids_in_exog = exog.keys()

    series_not_in_exog = set(series_names_in_) - set(series_ids_in_exog)
    if series_not_in_exog:
        warnings.warn(
            f"No `exog` for series {series_not_in_exog}. All values "
            f"of the exogenous variables for these series will be NaN.",
            MissingExogWarning
        )

    for k, v in exog_dict.items():
        if v is not None:
            check_exog(exog=v, allow_nan=True)
            if isinstance(v, pd.Series):
                v = v.to_frame()
            exog_dict[k] = v

    not_valid_index = [
        k
        for k, v in exog_dict.items()
        if v is not None and not isinstance(v.index, series_index_type)
    ]
    if not_valid_index:
        raise TypeError(
            f"All exog must have the same index type as `series`, which can be "
            f"either a pandas RangeIndex or a pandas DatetimeIndex. If either "
            f"`series` or `exog` is a pandas DataFrame with a MultiIndex, then "
            f"both must be pandas DatetimeIndex. Review exog for series: {not_valid_index}."
        )

    if isinstance(exog, dict):
        # NOTE: Check that all exog have the same dtypes for common columns
        exog_dtypes_buffer = pd.DataFrame(
            {k: df.dtypes for k, df in exog_dict.items() if df is not None}
        )
        exog_dtypes_nunique = exog_dtypes_buffer.nunique(axis=1)
        if not (exog_dtypes_nunique == 1).all():
            non_unique_dtypes_exogs = exog_dtypes_nunique[exog_dtypes_nunique != 1].index.to_list()
            raise TypeError(
                f"Exog/s: {non_unique_dtypes_exogs} have different dtypes in different "
                f"series. If any of these variables are categorical, note that this "
                f"error can also occur when their internal categories "
                f"(`series.cat.categories`) differ between series. Please ensure "
                f"that all series have the same categories (and category order) "
                f"for each categorical variable."
            )

        exog_names_in_ = list(
            set(
                column
                for df in exog_dict.values()
                if df is not None
                for column in df.columns.to_list()
            )
        )
    else:
        exog_names_in_ = list(exog.columns) if isinstance(exog, pd.DataFrame) else [exog.name]

    if len(set(exog_names_in_) - set(series_names_in_)) != len(exog_names_in_):
        raise ValueError(
            f"`exog` cannot contain a column named the same as one of the series.\n"
            f"    `series` columns : {series_names_in_}.\n"
            f"    `exog`   columns : {exog_names_in_}."
        )

    return exog_dict, exog_names_in_

skforecast.utils.utils.align_series_and_exog_multiseries

align_series_and_exog_multiseries(
    series_dict, exog_dict, trim_series_nan=True
)

Align series and exog according to their index. If needed, reindexing is applied. Heading and trailing NaNs are removed from all series in series_dict when trim_series_nan is True.

Parameters:

Name Type Description Default
series_dict dict

Dictionary with the series used during training.

required
exog_dict dict

Dictionary with the exogenous variable/s used during training.

None
trim_series_nan bool

If True, leading and trailing NaNs are removed from each series and exog is reindexed accordingly. If False, NaN trimming is skipped and only exog reindexing is performed.

True

Returns:

Name Type Description
series_dict dict

Dictionary with the series used during training.

exog_dict dict

Dictionary with the exogenous variable/s used during training.

Source code in skforecast/utils/utils.py
3649
3650
3651
3652
3653
3654
3655
3656
3657
3658
3659
3660
3661
3662
3663
3664
3665
3666
3667
3668
3669
3670
3671
3672
3673
3674
3675
3676
3677
3678
3679
3680
3681
3682
3683
3684
3685
3686
3687
3688
3689
3690
3691
3692
3693
3694
3695
3696
3697
3698
3699
3700
3701
3702
3703
3704
3705
3706
3707
3708
3709
3710
3711
def align_series_and_exog_multiseries(
    series_dict: dict[str, pd.Series],
    exog_dict: dict[str, pd.DataFrame | None],
    trim_series_nan: bool = True,
) -> tuple[dict[str, pd.Series], dict[str, pd.DataFrame | None]]:
    """
    Align series and exog according to their index. If needed, reindexing is
    applied. Heading and trailing NaNs are removed from all series in 
    `series_dict` when `trim_series_nan` is `True`.

    Parameters
    ----------
    series_dict : dict
        Dictionary with the series used during training.
    exog_dict : dict, default None
        Dictionary with the exogenous variable/s used during training.
    trim_series_nan : bool, default True
        If `True`, leading and trailing NaNs are removed from each series
        and exog is reindexed accordingly. If `False`, NaN trimming is
        skipped and only exog reindexing is performed.

    Returns
    -------
    series_dict : dict
        Dictionary with the series used during training.
    exog_dict : dict
        Dictionary with the exogenous variable/s used during training.

    """

    for k in series_dict.keys():
        if trim_series_nan and (
            np.isnan(series_dict[k].iat[0]) or np.isnan(series_dict[k].iat[-1])
        ):
            first_valid_index = series_dict[k].first_valid_index()
            last_valid_index = series_dict[k].last_valid_index()
            series_dict[k] = series_dict[k].loc[first_valid_index : last_valid_index]
        else:
            first_valid_index = series_dict[k].index[0]
            last_valid_index = series_dict[k].index[-1]

        if exog_dict[k] is not None:
            if not series_dict[k].index.equals(exog_dict[k].index):
                exog_dict[k] = exog_dict[k].loc[first_valid_index:last_valid_index]
                if exog_dict[k].empty:
                    warnings.warn(
                        f"`exog` for series '{k}' is empty after aligning "
                        f"with the series index. Exog values will be NaN.",
                        MissingValuesWarning
                    )
                    exog_dict[k] = None
                elif len(exog_dict[k]) != len(series_dict[k]):
                    warnings.warn(
                        f"`exog` for series '{k}' doesn't have values for "
                        f"all the dates in the series. Missing values will be "
                        f"filled with NaN.",
                        MissingValuesWarning
                    )
                    exog_dict[k] = exog_dict[k].reindex(
                        series_dict[k].index, fill_value = np.nan
                    )

    return series_dict, exog_dict

skforecast.utils.utils.prepare_levels_multiseries

prepare_levels_multiseries(
    X_train_series_names_in_, levels=None
)

Prepare list of levels to be predicted in multiseries Forecasters.

Parameters:

Name Type Description Default
X_train_series_names_in_ list

Names of the series (levels) included in the matrix X_train.

required
levels (str, list)

Names of the series (levels) to be predicted.

None

Returns:

Name Type Description
levels list

Names of the series (levels) to be predicted.

input_levels_is_list bool

Indicates if input levels argument is a list.

Source code in skforecast/utils/utils.py
3714
3715
3716
3717
3718
3719
3720
3721
3722
3723
3724
3725
3726
3727
3728
3729
3730
3731
3732
3733
3734
3735
3736
3737
3738
3739
3740
3741
3742
3743
3744
3745
def prepare_levels_multiseries(
    X_train_series_names_in_: list[str],
    levels: str | list[str] | None = None
) -> tuple[list[str], bool]:
    """
    Prepare list of levels to be predicted in multiseries Forecasters.

    Parameters
    ----------
    X_train_series_names_in_ : list
        Names of the series (levels) included in the matrix `X_train`.
    levels : str, list, default None
        Names of the series (levels) to be predicted.

    Returns
    -------
    levels : list
        Names of the series (levels) to be predicted.
    input_levels_is_list : bool
        Indicates if input levels argument is a list.

    """

    input_levels_is_list = False
    if levels is None:
        levels = X_train_series_names_in_
    elif isinstance(levels, str):
        levels = [levels]
    else:
        input_levels_is_list = True

    return levels, input_levels_is_list

skforecast.utils.utils.preprocess_levels_self_last_window_multiseries

preprocess_levels_self_last_window_multiseries(
    levels, input_levels_is_list, last_window_
)

Preprocess levels and last_window (when using self.last_window_) arguments in multiseries Forecasters when predicting. Only levels whose last window ends at the same datetime index will be predicted together.

Parameters:

Name Type Description Default
levels list

Names of the series (levels) to be predicted.

required
input_levels_is_list bool

Indicates if input levels argument is a list.

required
last_window_ dict

Dictionary with the last window of each series (self.last_window_).

required

Returns:

Name Type Description
levels list

Names of the series (levels) to be predicted.

last_window pandas DataFrame

Series values used to create the predictors (lags) needed in the first iteration of the prediction (t + 1).

Source code in skforecast/utils/utils.py
3748
3749
3750
3751
3752
3753
3754
3755
3756
3757
3758
3759
3760
3761
3762
3763
3764
3765
3766
3767
3768
3769
3770
3771
3772
3773
3774
3775
3776
3777
3778
3779
3780
3781
3782
3783
3784
3785
3786
3787
3788
3789
3790
3791
3792
3793
3794
3795
3796
3797
3798
3799
3800
3801
3802
3803
3804
3805
3806
3807
3808
3809
3810
3811
3812
3813
3814
3815
3816
3817
3818
3819
3820
3821
3822
3823
3824
3825
3826
3827
3828
3829
3830
def preprocess_levels_self_last_window_multiseries(
    levels: list[str],
    input_levels_is_list: bool,
    last_window_: dict[str, pd.Series],
) -> tuple[list[str], pd.DataFrame]:
    """
    Preprocess `levels` and `last_window` (when using self.last_window_) arguments 
    in multiseries Forecasters when predicting. Only levels whose last window 
    ends at the same datetime index will be predicted together.

    Parameters
    ----------
    levels : list
        Names of the series (levels) to be predicted.
    input_levels_is_list : bool
        Indicates if input levels argument is a list.
    last_window_ : dict
        Dictionary with the last window of each series (self.last_window_).

    Returns
    -------
    levels : list
        Names of the series (levels) to be predicted.
    last_window : pandas DataFrame
        Series values used to create the predictors (lags) needed in the 
        first iteration of the prediction (t + 1).

    """

    available_last_windows = set() if last_window_ is None else set(last_window_.keys())
    not_available_last_window = set(levels) - available_last_windows
    if not_available_last_window:
        levels = [
            level for level in levels 
            if level not in not_available_last_window
        ]
        if not levels:
            raise ValueError(
                f"No series to predict. None of the series {not_available_last_window} "
                f"are present in `last_window_` attribute. Provide `last_window` "
                f"as argument in predict method."
            )
        else:
            warnings.warn(
                f"Levels {not_available_last_window} are excluded from "
                f"prediction since they were not stored in `last_window_` "
                f"attribute during training. If you don't want to retrain "
                f"the Forecaster, provide `last_window` as argument.",
                IgnoredArgumentWarning
            )

    last_index_levels = [
        v.index[-1] 
        for k, v in last_window_.items()
        if k in levels
    ]
    if len(set(last_index_levels)) > 1:
        max_index_levels = max(last_index_levels)
        selected_levels = [
            k
            for k, v in last_window_.items()
            if k in levels and v.index[-1] == max_index_levels
        ]

        series_excluded_from_last_window = set(levels) - set(selected_levels)
        levels = selected_levels

        if input_levels_is_list and series_excluded_from_last_window:
            warnings.warn(
                f"Only series whose last window ends at the same index "
                f"can be predicted together. Series that do not reach "
                f"the maximum index, '{max_index_levels}', are excluded "
                f"from prediction: {series_excluded_from_last_window}.",
                IgnoredArgumentWarning
            )

    last_window = pd.DataFrame(
        {k: v 
         for k, v in last_window_.items() 
         if k in levels}
    )

    return levels, last_window

skforecast.utils.utils.prepare_steps_direct

prepare_steps_direct(max_step, steps=None)

Prepare list of steps to be predicted in Direct Forecasters.

Parameters:

Name Type Description Default
max_step int, list, numpy ndarray

Maximum number of future steps the forecaster will predict when using predict methods.

required
steps (int, list, None)

Predict n steps. The value of steps must be less than or equal to the value of steps defined when initializing the forecaster. Starts at 1.

  • If int: Only steps within the range of 1 to int are predicted.
  • If list: List of ints. Only the steps contained in the list are predicted.
  • If None: As many steps are predicted as were defined at initialization.
None

Returns:

Name Type Description
steps_direct list

Steps to be predicted.

Source code in skforecast/utils/utils.py
3833
3834
3835
3836
3837
3838
3839
3840
3841
3842
3843
3844
3845
3846
3847
3848
3849
3850
3851
3852
3853
3854
3855
3856
3857
3858
3859
3860
3861
3862
3863
3864
3865
3866
3867
3868
3869
3870
3871
3872
3873
3874
3875
3876
3877
3878
3879
def prepare_steps_direct(
    max_step: int | list[int] | np.ndarray[int],
    steps: int | list[int] | None = None
) -> list[int]:
    """
    Prepare list of steps to be predicted in Direct Forecasters.

    Parameters
    ----------
    max_step : int, list, numpy ndarray
        Maximum number of future steps the forecaster will predict 
        when using predict methods.
    steps : int, list, None, default None
        Predict n steps. The value of `steps` must be less than or equal to the 
        value of steps defined when initializing the forecaster. Starts at 1.

        - If `int`: Only steps within the range of 1 to int are predicted.
        - If `list`: List of ints. Only the steps contained in the list 
        are predicted.
        - If `None`: As many steps are predicted as were defined at 
        initialization.

    Returns
    -------
    steps_direct : list
        Steps to be predicted.

    """

    if isinstance(steps, int):
        steps_direct = list(range(1, steps + 1))
    elif steps is None:
        if isinstance(max_step, int):
            steps_direct = list(range(1, max_step + 1))
        else:
            steps_direct = [int(s) for s in max_step]
    elif isinstance(steps, list):
        steps_direct = []
        for step in steps:
            if not isinstance(step, (int, np.integer)):
                raise TypeError(
                    f"`steps` argument must be an int, a list of ints or `None`. "
                    f"Got {type(steps)}."
                )
            steps_direct.append(int(step))

    return steps_direct

skforecast.utils.utils.scale_correction_factor_differentiation

scale_correction_factor_differentiation(
    correction_factor, steps, differentiation_order
)

Scale the conformal prediction correction factor to account for the variance growth introduced by inverting the differentiation. When differentiation of order d is reverted via cumulative sums, a constant correction factor would accumulate linearly, producing intervals that grow as h instead of the theoretically correct growth rate.

The scaling is derived from the MA(infinity) representation of the inverse difference operator (1-B)^{-d}, whose coefficients are psi_j = comb(j + d - 1, d - 1). The correction factor at step h is scaled by sqrt(sum_{j=0}^{h-1} psi_j^2), which for d=1 simplifies to sqrt(h).

Parameters:

Name Type Description Default
correction_factor float, numpy ndarray

Correction factor from the conformal prediction method. Can be a scalar (non-binned residuals) or a 1D array with one value per step (binned residuals).

required
steps int

Number of forecast steps.

required
differentiation_order int

Order of differentiation applied to the series.

required

Returns:

Name Type Description
correction_factor_scaled numpy ndarray

Scaled correction factor with one value per step.

Source code in skforecast/utils/utils.py
4155
4156
4157
4158
4159
4160
4161
4162
4163
4164
4165
4166
4167
4168
4169
4170
4171
4172
4173
4174
4175
4176
4177
4178
4179
4180
4181
4182
4183
4184
4185
4186
4187
4188
4189
4190
4191
4192
4193
4194
4195
4196
4197
4198
4199
def scale_correction_factor_differentiation(
    correction_factor: float | np.ndarray,
    steps: int,
    differentiation_order: int
) -> np.ndarray:
    """
    Scale the conformal prediction correction factor to account for the
    variance growth introduced by inverting the differentiation. When
    differentiation of order `d` is reverted via cumulative sums, a constant
    correction factor would accumulate linearly, producing intervals that
    grow as `h` instead of the theoretically correct growth rate.

    The scaling is derived from the MA(infinity) representation of the
    inverse difference operator `(1-B)^{-d}`, whose coefficients are
    `psi_j = comb(j + d - 1, d - 1)`. The correction factor at step `h`
    is scaled by `sqrt(sum_{j=0}^{h-1} psi_j^2)`, which for `d=1`
    simplifies to `sqrt(h)`.

    Parameters
    ----------
    correction_factor : float, numpy ndarray
        Correction factor from the conformal prediction method. Can be a
        scalar (non-binned residuals) or a 1D array with one value per step
        (binned residuals).
    steps : int
        Number of forecast steps.
    differentiation_order : int
        Order of differentiation applied to the series.

    Returns
    -------
    correction_factor_scaled : numpy ndarray
        Scaled correction factor with one value per step.

    """

    steps_array = np.arange(1, steps + 1)
    scaling_factor = np.sqrt(
        np.cumsum(
            comb(steps_array + differentiation_order - 2, differentiation_order - 1)
            ** 2
        )
    )

    return correction_factor * scaling_factor