Skip to content

Datasets¶

skforecast.datasets.fetch_dataset ¶

fetch_dataset(
    name,
    version=None,
    raw=False,
    kwargs_read=None,
    verbose=True,
)

Fetch a dataset from the skforecast-datasets repository.

Parameters:

Name Type Description Default
name str

Name of the dataset to fetch.

required
version (str, int)

Deprecated since version 0.27.0, it has no effect and will be removed in version 0.29.0. Datasets are always fetched from the main branch of skforecast-datasets.

None
raw bool

If True, the raw dataset is fetched. If False, the preprocessed dataset is fetched: the column with the date/time is set as index, converted to datetime and sorted. When the dates are unique, the frequency of the dataset is set with asfreq (rows with NaN are introduced for missing dates, with a MissingValuesWarning).

False
kwargs_read dict

Kwargs to pass to pandas pd.read_csv or pd.read_parquet function depending on the dataset file type.

None
verbose bool

If True, print information about the dataset.

True

Returns:

Name Type Description
df pandas DataFrame

Dataset.

Notes

Datasets with several series in long format (one row per series and date, see their description in show_datasets_info) have repeated dates in the index, so no frequency is set and the rows are sorted by date, keeping the order of the series within each date. Use reshape_series_long_to_dict (after reset_index, it needs the dates as a column) or pivot_table to get one series per key or column.

Source code in skforecast/datasets/datasets.py
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
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
866
867
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
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
def fetch_dataset(
    name: str,
    version: str | int | None = None,
    raw: bool = False,
    kwargs_read: dict | None = None,
    verbose: bool = True
) -> pd.DataFrame:
    """
    Fetch a dataset from the skforecast-datasets repository.

    Parameters
    ----------
    name : str
        Name of the dataset to fetch.
    version : str, int, default None
        **Deprecated** since version 0.27.0, it has no effect and will be removed
        in version 0.29.0. Datasets are always fetched from the main branch of
        skforecast-datasets.
    raw : bool, default False
        If `True`, the raw dataset is fetched. If `False`, the preprocessed
        dataset is fetched: the column with the date/time is set as index,
        converted to datetime and sorted. When the dates are unique, the
        frequency of the dataset is set with `asfreq` (rows with NaN are
        introduced for missing dates, with a `MissingValuesWarning`).
    kwargs_read : dict, default None
        Kwargs to pass to pandas `pd.read_csv` or `pd.read_parquet` function
        depending on the dataset file type.
    verbose : bool, default True
        If `True`, print information about the dataset.

    Returns
    -------
    df : pandas DataFrame
        Dataset.

    Notes
    -----
    Datasets with several series in long format (one row per series and date,
    see their description in `show_datasets_info`) have repeated dates in the
    index, so no frequency is set and the rows are sorted by date, keeping the
    order of the series within each date. Use `reshape_series_long_to_dict`
    (after `reset_index`, it needs the dates as a column) or `pivot_table` to
    get one series per key or column.

    """

    _warn_deprecated_version(version)
    kwargs_read = kwargs_read or {}

    if name not in datasets:
        raise ValueError(
            f"Dataset '{name}' not found. "
            f"Available datasets are: {sorted(datasets)}"
        )

    file_type = datasets[name]['file_type']
    if file_type not in ('csv', 'parquet'):
        raise ValueError(
            f"Unknown file type '{file_type}' for dataset '{name}'. "
            f"Supported file types are 'csv' and 'parquet'."
        )

    url = datasets[name]['url']
    if '{version}' in url:
        url = url.format(version='main')

    if file_type == 'csv':
        kwargs_read = {'sep': datasets[name]['sep'], **kwargs_read}
        try:
            df = pd.read_csv(url, **kwargs_read)
        except Exception as e:
            raise ValueError(
                f"Error reading dataset '{name}' from {url}: {str(e)}."
            ) from e
    elif file_type == 'parquet':
        try:
            df = pd.read_parquet(url, **kwargs_read)
        except ImportError as e:
            raise ImportError(
                f"Reading dataset '{name}' requires pyarrow or fastparquet: "
                f"pip install pyarrow. Original error: {e}"
            ) from e
        except Exception as e:
            raise ValueError(
                f"Error reading dataset '{name}' from {url}: {str(e)}."
            ) from e
        # pyarrow keeps the memory used while reading in its pool. Returning it
        # to the OS lets the preprocessing of large datasets (e.g. m5) fit in
        # memory.
        try:
            import pyarrow as pa
            pa.default_memory_pool().release_unused()
        except ImportError:
            pass

    if not raw:
        n_inserted = n_dropped = 0
        try:
            index_col = datasets[name]['index_col']
            freq = datasets[name]['freq']
            date_format = datasets[name]['date_format']
            if df.index.name != index_col:
                # Unlike `set_index`, `pop` does not copy the whole DataFrame
                df.index = df.pop(index_col)
            df.index = pd.to_datetime(df.index, format=date_format)
            if df.index.is_unique:
                index_before = df.index
                df = df.asfreq(freq)
                n_inserted = len(df.index.difference(index_before))
                n_dropped = len(index_before.difference(df.index))
            if not df.index.is_monotonic_increasing:
                # Stable, to keep the order of the series of long-format datasets
                # within each date
                df = df.sort_index(kind='stable')
        except Exception as e:
            warnings.warn(
                f"Could not preprocess dataset '{name}': {e}",
                stacklevel=2
            )

        # Outside the `try`, so that a warning turned into an error is not
        # reported as a preprocessing failure
        if n_inserted > 0:
            warnings.warn(
                f"Dataset '{name}' has {n_inserted} missing dates for frequency "
                f"'{freq}'. Rows with NaN values have been introduced after "
                f"setting the frequency.",
                MissingValuesWarning,
                stacklevel=2
            )
        if n_dropped > 0:
            warnings.warn(
                f"{n_dropped} rows of dataset '{name}' are not aligned with "
                f"frequency '{freq}' and have been dropped after setting the "
                f"frequency. The frequency of the dataset may be wrong, use "
                f"`raw=True` to get the original data.",
                stacklevel=2
            )

    if verbose:
        _print_dataset_info(name, shape=df.shape)

    return df

skforecast.datasets.load_demo_dataset ¶

load_demo_dataset(version=None, verbose=True)

Load demo data set with monthly expenditure ($AUD) on corticosteroid drugs that the Australian health system had between 1991 and 2008. Obtained from the book: Forecasting: Principles and Practice by Rob J Hyndman and George Athanasopoulos. Index is set to datetime with monthly frequency and sorted.

Parameters:

Name Type Description Default
version (str, int)

Deprecated since version 0.27.0, it has no effect and will be removed in version 0.29.0. Datasets are always fetched from the main branch of skforecast-datasets.

None
verbose bool

If True, print information about the dataset.

True

Returns:

Name Type Description
y pandas Series

Dataset.

Source code in skforecast/datasets/datasets.py
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
def load_demo_dataset(
    version: str | int | None = None,
    verbose: bool = True
) -> pd.Series:
    """
    Load demo data set with monthly expenditure ($AUD) on corticosteroid drugs that
    the Australian health system had between 1991 and 2008. Obtained from the book:
    Forecasting: Principles and Practice by Rob J Hyndman and George Athanasopoulos.
    Index is set to datetime with monthly frequency and sorted.

    Parameters
    ----------
    version : str, int, default None
        **Deprecated** since version 0.27.0, it has no effect and will be removed
        in version 0.29.0. Datasets are always fetched from the main branch of
        skforecast-datasets.
    verbose : bool, default True
        If `True`, print information about the dataset.

    Returns
    -------
    y : pandas Series
        Dataset.

    """

    _warn_deprecated_version(version)

    # `version` is not passed, so the deprecation warning is raised only once
    y = fetch_dataset('h2o', raw=False, verbose=False)['x']
    y = y.rename('y')
    y.index.name = 'datetime'

    if verbose:
        _print_dataset_info('h2o', shape=y.shape)

    return y

skforecast.datasets.show_datasets_info ¶

show_datasets_info(datasets_names=None, version=None)

Print information about available datasets. If datasets_names is provided, only information about those datasets will be printed.

Parameters:

Name Type Description Default
datasets_names (str, list)

Name or list of names of the datasets to display information about. If None, information about all datasets will be displayed.

None
version (str, int)

Deprecated since version 0.27.0, it has no effect and will be removed in version 0.29.0. Datasets are always fetched from the main branch of skforecast-datasets.

None

Returns:

Type Description
None
Source code in skforecast/datasets/datasets.py
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
def show_datasets_info(
    datasets_names: str | list[str] | None = None,
    version: str | int | None = None
) -> None:
    """
    Print information about available datasets. If `datasets_names` is provided,
    only information about those datasets will be printed.

    Parameters
    ----------
    datasets_names : str, list, default None
        Name or list of names of the datasets to display information about. If
        None, information about all datasets will be displayed.
    version : str, int, default None
        **Deprecated** since version 0.27.0, it has no effect and will be removed
        in version 0.29.0. Datasets are always fetched from the main branch of
        skforecast-datasets.

    Returns
    -------
    None

    """

    _warn_deprecated_version(version)
    if datasets_names is None:
        datasets_names = sorted(datasets)
    elif isinstance(datasets_names, str):
        datasets_names = [datasets_names]
    else:
        datasets_names = list(datasets_names)

    for dataset_name in datasets_names:
        if dataset_name in datasets:
            _print_dataset_info(dataset_name)
        else:
            print(
                f"Dataset '{dataset_name}' not available. Set argument "
                f"`datasets_names` to None to see all available datasets."
            )