df = pl.DataFrame(
{"d": [date(2023, 12, 24), date(2023, 12, 25), date(2024, 1, 1)]}
)
df| d |
|---|
| date |
| 2023-12-24 |
| 2023-12-25 |
| 2024-01-01 |
Add binary indicator columns flagging named holidays and custom recurrences.
Usage
Each selected Date/Datetime column gets one companion Int32 column per requested holiday name and per requested recurrence rule, named {column}_{name} (the holiday or rule name lowercased with non-alphanumeric characters collapsed to underscores), that is 1 where the value falls on that holiday or matches that rule and 0 otherwise. A Datetime value is compared by its date part; time of day never affects the result. For calendar parts such as month or day of week, see StepDateFeatures and StepDatetimeFeatures.
Named holidays (holidays) are looked up in the holidays PyPI package’s calendar for country (and, if given, subdiv), rebuilt from the actual year span present in the data being transformed, so dates outside the training year range are still classified correctly. A holiday name is only meaningful relative to the country/subdiv it was requested with; some public holidays shift onto an adjacent weekday and are reported under a separate "... (observed)" name, which only matches if requested explicitly.
Custom recurrences (rules) are dateutil.rrule.rrule or dateutil.rrule.rruleset objects the caller builds directly (weekends, “last Friday of the month”, Easter, and so on), evaluated the same way, over the year span present in the data being transformed. A rule constructed without an explicit dtstart only starts recurring from the moment it was built, so it should be given one early enough to cover the data it is matched against.
See Advanced Features in Feature Engineering A to Z for background on indicators for irregular events such as holidays.
See Arguments shared across steps for the arguments this step shares with the others.
columns: SelectorColumns to flag. Accepts a single column name, a list of names, a mlfeatures selector such as Selectors.date_features() (see Selectors for the full set), or a narwhals or polars selector (narwhals.selectors.by_dtype(narwhals.Date)). Selectors are resolved against the data frame’s schema when the recipe is fitted, so the same selector works on any backend. Only Date and Datetime columns are meaningful here.
holidays: list[str] | tuple[str, …] = _DEFAULT_HOLIDAYS
Holiday names to flag, matched against the holidays package’s calendar for country/subdiv. Default ("New Year's Day", "Labor Day", "Christmas Day").
rules: Mapping[str, rrule | rruleset] | None = NoneCustom recurrences to flag, keyed by name, each a dateutil.rrule.rrule or dateutil.rrule.rruleset. Default None (no custom recurrences). A name here must not collapse to the same column-safe slug as a holidays name or another rules name.
country: str = "US"holidays package country code the holiday names are resolved against. Default "US".
subdiv: str | None = NoneSubdivision code within country (e.g. a US state or Canadian province code), for holidays observed only in part of a country. Default None.
keep_original_cols: bool = FalseIf True, the source columns are kept alongside their indicator columns instead of being dropped. Default False.
role: str | None = "feature"Role assigned to the new indicator columns (see Roles). Default "feature".
id: str | None = None"holiday"; a second step of this type in the same recipe becomes "holiday_2", and passing an id already used in the recipe is an error.
Each selected column gets one indicator column per requested holiday:
| d |
|---|
| date |
| 2023-12-24 |
| 2023-12-25 |
| 2024-01-01 |
Specifying the step touches no data:
Recipe (untrained)
Inputs: no roles assigned
Steps:
step_holiday(select(role='feature', type=Date)) [untrained]
fit() validates the holiday names against a real calendar built from the training data’s years:
Recipe (trained)
Inputs: 1 columns (1 feature)
Steps:
step_holiday(d → d_christmas_day) [trained]
transform() adds one indicator column per requested holiday and drops the source:
keep_original_cols keeps the source column alongside the indicators:
recipe = Recipe().add(
StepHoliday(
s.date_features(), holidays=["Christmas Day"], keep_original_cols=True
)
)
fitted = recipe.fit(df)
fitted.transform(df)| d | d_christmas_day |
|---|---|
| date | i32 |
| 2023-12-24 | 0 |
| 2023-12-25 | 1 |
| 2024-01-01 | 0 |
country resolves the same holiday names against a different calendar:
recipe = Recipe().add(
StepHoliday(s.date_features(), holidays=["Christmas Day"], country="CA")
)
fitted = recipe.fit(df)
fitted.transform(df)| d_christmas_day |
|---|
| i32 |
| 0 |
| 1 |
| 0 |
rules flags custom recurrences, defined with dateutil.rrule, alongside named holidays:
from dateutil.rrule import rrule, WEEKLY, SA, SU
weekend = rrule(WEEKLY, byweekday=(SA, SU), dtstart=date(2023, 1, 1))
recipe = Recipe().add(
StepHoliday(
s.date_features(), holidays=["Christmas Day"], rules={"weekend": weekend}
)
)
fitted = recipe.fit(df)
fitted.transform(df)| d_christmas_day | d_weekend |
|---|---|
| i32 | i32 |
| 0 | 1 |
| 1 | 0 |
| 0 | 0 |