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

# order_categories

> (Re-)order the categories of a categorical column. 

The output, if transformation is successful, will always be an ordinal (ordered `category`) column,
even if the input was an unordered `category` or not a `Category` at all. I.e. it is supposed
that re-ordering the categories means the order is important.

If the input column is already a `category`:

* If unordered (non-ordinal): categories will be ordered in the given order (converted to ordinal)
* If ordered (ordinal): categories will be re-ordered only

In both cases, the specified categories have to match the ones already existing. I.e. only re-ordering
is allowed, but not deletion or addition of new categories.

If the column is *not* already a `category`:

* the column will be converted if the param `force_categorical` is `true`, and ordered as desired. Otherwise the new column will be identical to the input (no ordering performed).

## 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">
      To arrange the categories "small", "medium", "large" ("S", "M", "L") in reverser order:

      ```stan theme={null}
      order_categories(ds.cat_col, {"categories": ["L", "M", "S"]}) -> (ds.ordinal)
      ```
    </Tab>

    <Tab title="Example 2">
      Converting a text column containing the strings "low", "medium" and "high" to an ordinal column:

      ```stan theme={null}
      order_categories(ds.text, {
          "categories": ["low", "medium", "high"],
          "force_categorical": true
      }) -> (ds.ordinal)
      ```
    </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}
      order_categories(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>
    A column to re-order or convert to ordinal (ordered `category`).
  </ParamField>
</Accordion>

<Accordion title="Outputs" icon="right-from-bracket">
  <ParamField path="output" type="column" required>
    An ordinal column.
  </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="categories" type="[array[['string', 'number']], null]">
    List with desired order of categories.
    If `null`, the unique and lexicographically sorted existing values in the input column will be used.

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

  <ParamField path="force_categorical" type="boolean" default="true">
    Whether to convert non-categorical input columns to `category` before ordering (otherwise will be unchanged).
  </ParamField>
</Accordion>
