Skip to main content

StepDateFeatures

Expand Date columns into calendar-part feature columns.

Usage

Source

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 (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.

Parameters

columns: Selector

Columns 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 = 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). 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:

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:

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() only resolves the selection:

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() adds one column per requested calendar part and drops the source:

fitted.transform(df)
shape: (3, 5)
d_yeard_monthd_dayd_dowd_doy
i32i32i32i32i32
20201131
20206151167
202012314366

A narrower features produces fewer columns:

recipe = Recipe().add(StepDateFeatures(s.date_features(), features=["year", "month"]))
fitted = recipe.fit(df)
fitted.transform(df)
shape: (3, 2)
d_yeard_month
i32i32
20201
20206
202012

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)
shape: (3, 6)
dd_yeard_monthd_dayd_dowd_doy
datei32i32i32i32i32
2020-01-0120201131
2020-06-1520206151167
2020-12-31202012314366

categorical names level names instead of raw numbers for month and dow:

recipe = Recipe().add(
    StepDateFeatures(s.date_features(), categorical=["month", "dow"])
)
fitted = recipe.fit(df)
fitted.transform(df)
shape: (3, 5)
d_yeard_monthd_dayd_dowd_doy
i32enumi32enumi32
2020"January"1"Wednesday"1
2020"June"15"Monday"167
2020"December"31"Thursday"366