## StepHoliday


Add binary indicator columns flagging named holidays and custom recurrences.


Usage

``` python
StepHoliday(
    columns,
    *,
    holidays=_DEFAULT_HOLIDAYS,
    rules=None,
    country="US",
    subdiv=None,
    keep_original_cols=False,
    role="feature",
    id=None
)
```


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](StepDateFeatures.md#mlfeatures.StepDateFeatures) and [StepDatetimeFeatures](StepDatetimeFeatures.md#mlfeatures.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](https://feaz-book.com/datetime-advanced) in *Feature Engineering A to Z* for background on indicators for irregular events such as holidays.

See [Arguments shared across steps](../user-guide/common-step-arguments.md) for the arguments this step shares with the others.


## Parameters


`columns: Selector`  
Columns to flag. Accepts a single column name, a list of names, a mlfeatures selector such as [Selectors.date_features()](Selectors.md#mlfeatures.Selectors.date_features) (see [Selectors](Selectors.md#mlfeatures.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 = None`  
Custom 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 = None`  
Subdivision 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 = ``False`  
If `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](Roles.md#mlfeatures.Roles)). Default `"feature"`.

`id: str | None = None`  
Stable step identifier. Defaults to `"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.


## Examples

Each selected column gets one indicator column per requested holiday:


``` python
df = pl.DataFrame(
    {"d": [date(2023, 12, 24), date(2023, 12, 25), date(2024, 1, 1)]}
)
df
```


shape: (3, 1)

| d          |
|------------|
| date       |
| 2023-12-24 |
| 2023-12-25 |
| 2024-01-01 |


Specifying the step touches no data:


``` python
recipe = Recipe().add(StepHoliday(s.date_features(), holidays=["Christmas Day"]))
recipe
```


    Recipe (untrained)
      Inputs: no roles assigned
      Steps:
        step_holiday(select(role='feature', type=Date))  [untrained]


[fit()](Step.fit.md#mlfeatures.Step.fit) validates the holiday names against a real calendar built from the training data's years:


``` python
fitted = recipe.fit(df)
fitted
```


    Recipe (trained)
      Inputs: 1 columns (1 feature)
      Steps:
        step_holiday(d → d_christmas_day)  [trained]


[transform()](Step.transform.md#mlfeatures.Step.transform) adds one indicator column per requested holiday and drops the source:


``` python
fitted.transform(df)
```


shape: (3, 1)

| d_christmas_day |
|-----------------|
| i32             |
| 0               |
| 1               |
| 0               |


[keep_original_cols](Step.keep_original_cols.md#mlfeatures.Step.keep_original_cols) keeps the source column alongside the indicators:


``` python
recipe = Recipe().add(
    StepHoliday(
        s.date_features(), holidays=["Christmas Day"], keep_original_cols=True
    )
)
fitted = recipe.fit(df)
fitted.transform(df)
```


shape: (3, 2)

| 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:


``` python
recipe = Recipe().add(
    StepHoliday(s.date_features(), holidays=["Christmas Day"], country="CA")
)
fitted = recipe.fit(df)
fitted.transform(df)
```


shape: (3, 1)

| d_christmas_day |
|-----------------|
| i32             |
| 0               |
| 1               |
| 0               |


`rules` flags custom recurrences, defined with `dateutil.rrule`, alongside named holidays:


``` python
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)
```


shape: (3, 2)

| d_christmas_day | d_weekend |
|-----------------|-----------|
| i32             | i32       |
| 0               | 1         |
| 1               | 0         |
| 0               | 0         |
