df = pl.DataFrame({"d": [date(2020, 1, 1), date(2020, 6, 15), date(2020, 12, 31)]})
df| d |
|---|
| date |
| 2020-01-01 |
| 2020-06-15 |
| 2020-12-31 |
Expand Date columns into calendar-part feature columns.
Usage
Each selected Date column is replaced by one integer column per requested entry of 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.
A Datetime column (carrying a time-of-day component) is not accepted here; use StepDatetimeFeatures for those.
See Value Extraction in Feature Engineering A to Z for background on this technique.
See Arguments shared across steps for the arguments this step shares with the others.
columns: SelectorColumns to expand. 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 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, 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.
keep_original_cols: bool = FalseIf 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). Default "feature".
id: str | None = None"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.
Each selected column is replaced by its calendar-part columns:
| d |
|---|
| date |
| 2020-01-01 |
| 2020-06-15 |
| 2020-12-31 |
Specifying the step touches no data:
Recipe (untrained)
Inputs: no roles assigned
Steps:
step_date_features(select(role='feature', type=Date)) [untrained]
There is nothing to estimate, so fit() only resolves the selection:
Recipe (trained)
Inputs: 5 columns (5 features)
Steps:
step_date_features(d → d_year, d_month, d_day, d_dow, d_doy) [trained]
transform() adds one column per requested calendar part and drops the source:
| 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 produces fewer columns:
recipe = Recipe().add(StepDateFeatures(s.date_features(), features=["year", "month"]))
fitted = recipe.fit(df)
fitted.transform(df)| d_year | d_month |
|---|---|
| i32 | i32 |
| 2020 | 1 |
| 2020 | 6 |
| 2020 | 12 |
keep_original_cols keeps the source column alongside the calendar parts:
recipe = Recipe().add(
StepDateFeatures(s.date_features(), keep_original_cols=True)
)
fitted = recipe.fit(df)
fitted.transform(df)| 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 names level names instead of raw numbers for month and dow: