> ## Documentation Index
> Fetch the complete documentation index at: https://docs.graphext.com/llms.txt
> Use this file to discover all available pages before exploring further.

# replace_missing

> Replace missing values (NaNs) with either a specified constant value or the result of a given function. 

## Usage

The following examples show how the step can be used in a recipe.

<Accordion title="Examples" icon="code" defaultOpen="true">
  <Tabs>
    <Tab title="Example 1">
      The following configuration fills all missing values with the string "unknown":

      ```stan theme={null}
      replace_missing(ds.occupation, {"value": "unknown"}) -> (ds.occupation_filled)
      ```
    </Tab>

    <Tab title="Example 2">
      The following configuration fills all missing values with the maximum of the column:

      ```stan theme={null}
      replace_missing(ds.numbers, {"function": "max"}) -> (ds.numbers_filled)
      ```
    </Tab>

    <Tab title="Signature">
      General syntax for using the step in a recipe. Shows the inputs and outputs the step is expected to receive and will produce respectively. For futher details see sections below.

      ```stan theme={null}
      replace_missing(input: column, {
          "param": value,
          ...
      }) -> (output: column)
      ```
    </Tab>
  </Tabs>
</Accordion>

## Inputs & Outputs

The following are the inputs expected by the step and the outputs it produces. These are generally
columns (`ds.first_name`), datasets (`ds` or `ds[["first_name", "last_name"]]`) or models (referenced
by name e.g. `"churn-clf"`).

<Accordion title="Inputs" icon="right-to-bracket">
  <ParamField path="input" type="column" required>
    An arbitrary column, potentially containing missing values (NaN).
  </ParamField>
</Accordion>

<Accordion title="Outputs" icon="right-from-bracket">
  <ParamField path="output" type="column" required>
    A copy of the input column where missing values have been replaced by a constant.
  </ParamField>
</Accordion>

## Configuration

The following parameters can be used to configure the behaviour of the step by including them in
a json object as the last "input" to the step, i.e. `step(..., {"param": "value", ...}) -> (output)`.

<Accordion title="Parameters" defaultOpen="true" icon="sliders">
  <ParamField path="value" type="[number, string, array[['number', 'string']]]">
    The constant to use to fill in missing values (normally of same type as original column).
    Can be a scalar value (with number or string type) or an array of values (number or string). If an array
    is passed it should have at least one item.

    <Accordion title="Array items">
      <ParamField path="Item" type="[number, string]">
        Each item in array.
      </ParamField>
    </Accordion>
  </ParamField>

  <ParamField path="function" type="string">
    Fill missing values with the result of a given function.
    The following functions can be used:

    * max: substitutes the NaN values with the maximum value of a numerical column.
    * min: substitutes the NaN values with the minimum value of a numerical column.
    * mean: substitutes the NaN values with the mean of a numerical column.
    * median: substitutes the NaN values with the median of a numerical column.
    * least\_freq: substitutes the NaN values with the least frequent value of a column.
    * most\_freq: substitutes the NaN values with the most frequent value of a column.
    * alphabetical\_first: substitutes the NaN values with the alphabetically first value of a categorical column.
    * alphabetical\_first: substitutes the NaN values with the alphabetically last value of a categorical column.
    * bfill: for each NaN value, uses the next valid observation to fill it.
    * ffill: for each NaN value, propagates the last valid observation forward to fill it.

    Values must be one of the following:

    `max` `min` `mean` `median` `least_freq` `most_freq` `alphabetical_first` `alphabetical_last` `bfill` `ffill`
  </ParamField>
</Accordion>
