Skip to main content

StepIndicateNa

Add a binary indicator column flagging missing values.

Usage

Source

StepIndicateNa(
    columns,
    *,
    keep_original_cols=False,
    role="feature",
    id=None,
)

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.

Parameters

columns: Selector

Columns 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 = 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). Default "feature".

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

Examples

Each selected column gets a companion _na indicator:

df = pl.DataFrame({"x": [1.0, None, 3.0], "g": ["a", "b", None]})
df
shape: (3, 2)
xg
f64str
1.0"a"
null"b"
3.0null

Specifying the step touches no data:

recipe = Recipe().add(StepIndicateNa(s.features(), keep_original_cols=True))
recipe
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:

fitted = recipe.fit(df)
fitted
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:

fitted.transform(df)
shape: (3, 4)
xx_nagg_na
f64i32stri32
1.00"a"0
null1"b"0
3.00null1

Without keep_original_cols, the source column is dropped instead:

recipe = Recipe().add(StepIndicateNa(s.features()))
fitted = recipe.fit(df)
fitted.transform(df)
shape: (3, 2)
x_nag_na
i32i32
00
10
01