prophet.make_holidays

 1# Copyright (c) Facebook, Inc. and its affiliates.
 2
 3# This source code is licensed under the MIT license found in the
 4# LICENSE file in the root directory of this source tree.
 5
 6from __future__ import annotations
 7
 8import numpy as np
 9import pandas as pd
10
11import holidays
12
13
14def get_country_holidays_class(country: str) -> type[holidays.HolidayBase]:
15    """Get class for a supported country.
16
17    Parameters
18    ----------
19    country: country code
20
21    Returns
22    -------
23    A valid country holidays class
24    """
25    substitutions = {
26        "TU": "TR",  # For compatibility with Turkey as 'TU' cases.
27    }
28
29    country = substitutions.get(country, country)
30    if not hasattr(holidays, country):
31        raise AttributeError(f"Holidays in {country} are not currently supported!")
32
33    return getattr(holidays, country)
34
35
36def get_holiday_names(country: str) -> set[str]:
37    """Return all possible holiday names of given country
38
39    Parameters
40    ----------
41    country: country name
42
43    Returns
44    -------
45    A set of all possible holiday names of given country
46    """
47    country_holidays = get_country_holidays_class(country)
48    return set(country_holidays(language="en_US", years=np.arange(1995, 2045)).values())
49
50
51def make_holidays_df(
52    year_list: list[int],
53    country: str,
54    province: str | None = None,
55    state: str | None = None,  # unused
56) -> pd.DataFrame:
57    """Make dataframe of holidays for given years and countries
58
59    Parameters
60    ----------
61    year_list: a list of years
62    country: country name
63    province: province name
64
65    Returns
66    -------
67    Dataframe with 'ds' and 'holiday', which can directly feed
68    to 'holidays' params in Prophet
69    """
70    country_holidays = get_country_holidays_class(country)
71    holidays = country_holidays(expand=False, language="en_US", subdiv=province, years=year_list)
72
73    holidays_df = pd.DataFrame(
74        [(date, holidays.get_list(date)) for date in holidays],
75        columns=["ds", "holiday"],
76    )
77    holidays_df = holidays_df.explode("holiday")
78    holidays_df.reset_index(inplace=True, drop=True)
79    holidays_df["ds"] = pd.to_datetime(holidays_df["ds"])
80
81    return holidays_df
def get_country_holidays_class(country: str) -> type[holidays.holiday_base.HolidayBase]:
15def get_country_holidays_class(country: str) -> type[holidays.HolidayBase]:
16    """Get class for a supported country.
17
18    Parameters
19    ----------
20    country: country code
21
22    Returns
23    -------
24    A valid country holidays class
25    """
26    substitutions = {
27        "TU": "TR",  # For compatibility with Turkey as 'TU' cases.
28    }
29
30    country = substitutions.get(country, country)
31    if not hasattr(holidays, country):
32        raise AttributeError(f"Holidays in {country} are not currently supported!")
33
34    return getattr(holidays, country)

Get class for a supported country.

Parameters
  • country (country code):
Returns
  • A valid country holidays class
def get_holiday_names(country: str) -> set[str]:
37def get_holiday_names(country: str) -> set[str]:
38    """Return all possible holiday names of given country
39
40    Parameters
41    ----------
42    country: country name
43
44    Returns
45    -------
46    A set of all possible holiday names of given country
47    """
48    country_holidays = get_country_holidays_class(country)
49    return set(country_holidays(language="en_US", years=np.arange(1995, 2045)).values())

Return all possible holiday names of given country

Parameters
  • country (country name):
Returns
  • A set of all possible holiday names of given country
def make_holidays_df( year_list: list[int], country: str, province: str | None = None, state: str | None = None) -> pandas.DataFrame:
52def make_holidays_df(
53    year_list: list[int],
54    country: str,
55    province: str | None = None,
56    state: str | None = None,  # unused
57) -> pd.DataFrame:
58    """Make dataframe of holidays for given years and countries
59
60    Parameters
61    ----------
62    year_list: a list of years
63    country: country name
64    province: province name
65
66    Returns
67    -------
68    Dataframe with 'ds' and 'holiday', which can directly feed
69    to 'holidays' params in Prophet
70    """
71    country_holidays = get_country_holidays_class(country)
72    holidays = country_holidays(expand=False, language="en_US", subdiv=province, years=year_list)
73
74    holidays_df = pd.DataFrame(
75        [(date, holidays.get_list(date)) for date in holidays],
76        columns=["ds", "holiday"],
77    )
78    holidays_df = holidays_df.explode("holiday")
79    holidays_df.reset_index(inplace=True, drop=True)
80    holidays_df["ds"] = pd.to_datetime(holidays_df["ds"])
81
82    return holidays_df

Make dataframe of holidays for given years and countries

Parameters
  • year_list (a list of years):

  • country (country name):

  • province (province name):

Returns
  • Dataframe with 'ds' and 'holiday', which can directly feed
  • to 'holidays' params in Prophet