df = pl.DataFrame({"x": [1.0, None, 3.0], "g": ["a", "b", None]})
df| x | g |
|---|---|
| f64 | str |
| 1.0 | "a" |
| null | "b" |
| 3.0 | null |
Add a binary indicator column flagging missing values.
Usage
Each selected column gets a companion {column}_na column that is 1 where the source value is missing and 0 otherwise. Nothing is estimated from the training data: missingness is an elementwise property of each row’s value, so the transform is identical on training and new data, and a column with no missing values at fit time still gets an indicator (new data may have missing values training data did not).
A float column is flagged missing on either a null or a NaN value; every other dtype has no NaN representation, so only null counts.
Unlike most steps that replace a column with several, keep_original_cols is usually worth setting to True here: the indicator alone discards the source column’s own value, and a downstream imputation step (StepImputeMean, StepImputeMedian, StepImputeMode, StepUnknown) still needs that column to fill in.
See Missing Values Indicators 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 flag. Accepts a single column name, a list of names, a mlfeatures selector such as Selectors.features() (see Selectors for the full set), or a narwhals or polars selector (narwhals.selectors.numeric(), polars.selectors.numeric()). Selectors are resolved against the data frame’s schema when the recipe is fitted, so the same selector works on any backend. Any dtype is accepted.
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"indicate_na"; a second step of this type in the same recipe becomes "indicate_na_2", and passing an id already used in the recipe is an error.
Each selected column gets a companion _na indicator:
| x | g |
|---|---|
| f64 | str |
| 1.0 | "a" |
| null | "b" |
| 3.0 | null |
Specifying the step touches no data:
Recipe (untrained)
Inputs: no roles assigned
Steps:
step_indicate_na(select(role='feature')) [untrained]
There is nothing to estimate, so fit() only resolves the selection:
Recipe (trained)
Inputs: 4 columns (4 features)
Steps:
step_indicate_na(x, g → x_na, g_na) [trained]
transform() adds one indicator column per selected column, keeping the source alongside it so it is still available to a later imputation step:
Without keep_original_cols, the source column is dropped instead: