## StepDateFeatures


Expand Date columns into calendar-part feature columns.


Usage

``` python
StepDateFeatures(
    columns,
    *,
    features=_DEFAULT_DATE_FEATURES,
    categorical=(),
    keep_original_cols=False,
    role="feature",
    id=None
)
```


Each selected Date column is replaced by one integer column per requested entry of [features](Selectors.md#mlfeatures.Selectors.features) (`year`, `month`, `day`, `dow`, `doy`), named `{column}_{feature}`. Nothing is estimated from the training data: every calendar part is an elementwise function of the column's value, so the transform is identical on training and new data.

`dow` is the ISO day of week (`1` = Monday, `7` = Sunday); `doy` is the ordinal day of year (`1`-`366`). Produced columns are `Int32` regardless of backend, unless named in [categorical](Selectors.md#mlfeatures.Selectors.categorical).

A Datetime column (carrying a time-of-day component) is not accepted here; use [StepDatetimeFeatures](StepDatetimeFeatures.md#mlfeatures.StepDatetimeFeatures) for those.

See [Value Extraction](https://feaz-book.com/datetime-extraction) in *Feature Engineering A to Z* for background on this technique.

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 expand. 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 columns are meaningful here.

`features: list[str] | tuple[str, …] = _DEFAULT_DATE_FEATURES`\  
Calendar parts to compute, from `"year"`, `"month"`, `"day"`, `"dow"` (day of week), and `"doy"` (day of year). Default `("year", "month", "day", "dow", "doy")`. Deduplicated and reordered to that same canonical order regardless of the order passed in, so the output columns are always in a fixed order.

`categorical: list[str] | tuple[str, …] = ()`\  
Subset of [features](Selectors.md#mlfeatures.Selectors.features), restricted to `"month"` and `"dow"` (the two parts with a natural, fixed vocabulary of level names), to encode as an ordered categorical column of level names (`"January"`, `"Monday"`, …) instead of a plain integer. Default `()`: no feature is categorical. Each entry must also be present in [features](Selectors.md#mlfeatures.Selectors.features).

`keep_original_cols: bool = ``False`  
If `True`, the source columns are kept alongside their calendar-part columns instead of being dropped. Default `False`.

`role: str | None = ``"feature"`  
Role assigned to the new calendar-part columns (see [Roles](Roles.md#mlfeatures.Roles)). Default `"feature"`.

`id: str | None = None`  
Stable step identifier. Defaults to `"date_features"`; a second step of this type in the same recipe becomes `"date_features_2"`, and passing an `id` already used in the recipe is an error.


## Examples

Each selected column is replaced by its calendar-part columns:


``` python
df = pl.DataFrame({"d": [date(2020, 1, 1), date(2020, 6, 15), date(2020, 12, 31)]})
df
```


shape: (3, 1)

| d          |
|------------|
| date       |
| 2020-01-01 |
| 2020-06-15 |
| 2020-12-31 |


Specifying the step touches no data:


``` python
recipe = Recipe().add(StepDateFeatures(s.date_features()))
recipe
```


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


There is nothing to estimate, so [fit()](Step.fit.md#mlfeatures.Step.fit) only resolves the selection:


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


    Recipe (trained)
      Inputs: 5 columns (5 features)
      Steps:
        step_date_features(d → d_year, d_month, d_day, d_dow, d_doy)  [trained]


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


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


shape: (3, 5)

| d_year | d_month | d_day | d_dow | d_doy |
|--------|---------|-------|-------|-------|
| i32    | i32     | i32   | i32   | i32   |
| 2020   | 1       | 1     | 3     | 1     |
| 2020   | 6       | 15    | 1     | 167   |
| 2020   | 12      | 31    | 4     | 366   |


A narrower [features](Selectors.md#mlfeatures.Selectors.features) produces fewer columns:


``` python
recipe = Recipe().add(StepDateFeatures(s.date_features(), features=["year", "month"]))
fitted = recipe.fit(df)
fitted.transform(df)
```


shape: (3, 2)

| d_year | d_month |
|--------|---------|
| i32    | i32     |
| 2020   | 1       |
| 2020   | 6       |
| 2020   | 12      |


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


``` python
recipe = Recipe().add(
    StepDateFeatures(s.date_features(), keep_original_cols=True)
)
fitted = recipe.fit(df)
fitted.transform(df)
```


shape: (3, 6)

| d          | d_year | d_month | d_day | d_dow | d_doy |
|------------|--------|---------|-------|-------|-------|
| date       | i32    | i32     | i32   | i32   | i32   |
| 2020-01-01 | 2020   | 1       | 1     | 3     | 1     |
| 2020-06-15 | 2020   | 6       | 15    | 1     | 167   |
| 2020-12-31 | 2020   | 12      | 31    | 4     | 366   |


[categorical](Selectors.md#mlfeatures.Selectors.categorical) names level names instead of raw numbers for `month` and `dow`:


``` python
recipe = Recipe().add(
    StepDateFeatures(s.date_features(), categorical=["month", "dow"])
)
fitted = recipe.fit(df)
fitted.transform(df)
```


shape: (3, 5)

| d_year | d_month    | d_day | d_dow       | d_doy |
|--------|------------|-------|-------------|-------|
| i32    | enum       | i32   | enum        | i32   |
| 2020   | "January"  | 1     | "Wednesday" | 1     |
| 2020   | "June"     | 15    | "Monday"    | 167   |
| 2020   | "December" | 31    | "Thursday"  | 366   |
