## StepIndicateNa


Add a binary indicator column flagging missing values.


Usage

``` python
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](Step.keep_original_cols.md#mlfeatures.Step.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](StepImputeMean.md#mlfeatures.StepImputeMean), [StepImputeMedian](StepImputeMedian.md#mlfeatures.StepImputeMedian), [StepImputeMode](StepImputeMode.md#mlfeatures.StepImputeMode), [StepUnknown](StepUnknown.md#mlfeatures.StepUnknown)) still needs that column to fill in.

See [Missing Values Indicators](https://feaz-book.com/missing-indicator) 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 flag. Accepts a single column name, a list of names, a mlfeatures selector such as [Selectors.features()](Selectors.md#mlfeatures.Selectors.features) (see [Selectors](Selectors.md#mlfeatures.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](Roles.md#mlfeatures.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:


``` python
df = pl.DataFrame({"x": [1.0, None, 3.0], "g": ["a", "b", None]})
df
```


shape: (3, 2)

| x    | g    |
|------|------|
| f64  | str  |
| 1.0  | "a"  |
| null | "b"  |
| 3.0  | null |


Specifying the step touches no data:


``` python
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()](Step.fit.md#mlfeatures.Step.fit) only resolves the selection:


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


    Recipe (trained)
      Inputs: 4 columns (4 features)
      Steps:
        step_indicate_na(x, g → x_na, g_na)  [trained]


[transform()](Step.transform.md#mlfeatures.Step.transform) adds one indicator column per selected column, keeping the source alongside it so it is still available to a later imputation step:


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


shape: (3, 4)

| x    | x_na | g    | g_na |
|------|------|------|------|
| f64  | i32  | str  | i32  |
| 1.0  | 0    | "a"  | 0    |
| null | 1    | "b"  | 0    |
| 3.0  | 0    | null | 1    |


Without [keep_original_cols](Step.keep_original_cols.md#mlfeatures.Step.keep_original_cols), the source column is dropped instead:


``` python
recipe = Recipe().add(StepIndicateNa(s.features()))
fitted = recipe.fit(df)
fitted.transform(df)
```


shape: (3, 2)

| x_na | g_na |
|------|------|
| i32  | i32  |
| 0    | 0    |
| 1    | 0    |
| 0    | 1    |
