## Selectors


Namespace of factory methods for built-in mlfeatures column selectors.


Usage

``` python
Selectors()
```


All factories return a [FeaturesSelector](FeaturesSelector.md#mlfeatures.FeaturesSelector) that composes with `&` / `|` / `~` / `-`.

Prefer a selector to a hard-coded list of column names. A list goes stale as soon as an earlier step adds or removes columns (a dummy step replaces `city` with `city_LA` and `city_NYC`), while a selector is resolved against the columns each step actually sees, so it keeps working when the data frame changes.


## Examples

A selector stands in for an explicit column list and is resolved against the data frame at fit time. Here `starts_with("x")` picks up `x1` and `x2` but leaves `y` untouched:


``` python
df = pd.DataFrame(
    {"x1": [1.0, 2.0, 3.0], "x2": [4.0, 5.0, 6.0], "y": [7.0, 8.0, 9.0]}
)
recipe = Recipe().add(StepNormalize(s.starts_with("x")))
fitted = recipe.fit(df)
fitted.transform(df)
```


|     | x1   | x2   | y   |
|-----|------|------|-----|
| 0   | -1.0 | -1.0 | 7.0 |
| 1   | 0.0  | 0.0  | 8.0 |
| 2   | 1.0  | 1.0  | 9.0 |


## Methods

| Name | Description |
|----|----|
| [boolean()](#boolean) | All boolean columns. |
| [boolean_features()](#boolean_features) | All feature columns that are boolean-typed. |
| [categorical()](#categorical) | All categorical/enum columns (ordered or unordered). |
| [categorical_features()](#categorical_features) | All feature columns that are categorical. |
| [col()](#col) | The named column(s), if present. |
| [contains()](#contains) | Columns whose name contains `substring` (case-sensitive). |
| [date()](#date) | All Date columns. |
| [date_features()](#date_features) | All feature columns that are Date-typed. |
| [datetime()](#datetime) | All Datetime columns. |
| [datetime_features()](#datetime_features) | All feature columns that are Datetime-typed. |
| [double_features()](#double_features) | All feature columns that are floating-point typed. |
| [ends_with()](#ends_with) | Columns whose name ends with `suffix`. |
| [everything()](#everything) | All columns. |
| [features()](#features) | All feature columns regardless of type. |
| [float()](#float) | All floating-point columns. |
| [has_ancestor()](#has_ancestor) | Columns with the given step **anywhere in their ancestry**. |
| [has_role()](#has_role) | All columns carrying `role`. |
| [has_source()](#has_source) | Columns whose **immediate** producer is the given step. |
| [integer()](#integer) | All integer columns (signed or unsigned). |
| [integer_features()](#integer_features) | All feature columns that are integer-typed. |
| [matches()](#matches) | Columns whose name matches the regular expression `pattern`. |
| [nominal()](#nominal) | All nominal columns (string, categorical, or enum). |
| [nominal_features()](#nominal_features) | All feature columns that are nominal (categorical, enum, or string). |
| [num_range()](#num_range) | Columns named `f"{prefix}{i}"` for `i` in `rng` (e.g. x1..x5). |
| [numeric()](#numeric) | All numeric columns: integer, floating-point, and decimal. |
| [numeric_features()](#numeric_features) | All feature columns that are numeric. |
| [ordered()](#ordered) | All ordered-categorical columns. |
| [starts_with()](#starts_with) | Columns whose name starts with `prefix`. |
| [string()](#string) | All string columns. |
| [string_features()](#string_features) | All feature columns that are string-typed. |
| [targets()](#targets) | All target columns regardless of type. |
| [unordered()](#unordered) | All unordered-categorical columns. |
| [wrap()](#wrap) | Lift a raw name / list / polars / narwhals selector into mlfeatures-land. |

------------------------------------------------------------------------


#### boolean()


All boolean columns.


Usage

``` python
boolean()
```


------------------------------------------------------------------------


#### boolean_features()


All feature columns that are boolean-typed.


Usage

``` python
boolean_features()
```


------------------------------------------------------------------------


#### categorical()


All categorical/enum columns (ordered or unordered).


Usage

``` python
categorical()
```


------------------------------------------------------------------------


#### categorical_features()


All feature columns that are categorical.


Usage

``` python
categorical_features()
```


------------------------------------------------------------------------


#### col()


The named column(s), if present.


Usage

``` python
col(*names)
```


Names that aren't in the data frame are silently skipped (tolerant, like R's `any_of`). This differs from passing a plain `list` of names as a selector operand, which is strict and raises on a missing column (like `all_of`); use [col](Selectors.md#mlfeatures.Selectors.col) when absence should be a no-op. A bare list that names a missing column raises the backend's own column-not-found error, not a mlfeatures one.


##### Examples

`"missing"` is not a column, so it is skipped and only `x1` is normalized:


``` python
recipe = Recipe().add(StepNormalize(s.col("x1", "missing")))
fitted = recipe.fit(df)
fitted.transform(df)
```


|     | x1   | x2  | y   |
|-----|------|-----|-----|
| 0   | -1.0 | 4.0 | 7.0 |
| 1   | 0.0  | 5.0 | 8.0 |
| 2   | 1.0  | 6.0 | 9.0 |


------------------------------------------------------------------------


#### contains()


Columns whose name contains `substring` (case-sensitive).


Usage

``` python
contains(substring)
```


------------------------------------------------------------------------


#### date()


All Date columns.


Usage

``` python
date()
```


------------------------------------------------------------------------


#### date_features()


All feature columns that are Date-typed.


Usage

``` python
date_features()
```


------------------------------------------------------------------------


#### datetime()


All Datetime columns.


Usage

``` python
datetime()
```


------------------------------------------------------------------------


#### datetime_features()


All feature columns that are Datetime-typed.


Usage

``` python
datetime_features()
```


------------------------------------------------------------------------


#### double_features()


All feature columns that are floating-point typed.


Usage

``` python
double_features()
```


------------------------------------------------------------------------


#### ends_with()


Columns whose name ends with `suffix`.


Usage

``` python
ends_with(suffix)
```


Matching is case-sensitive and takes a single suffix, like [starts_with](Selectors.md#mlfeatures.Selectors.starts_with).


------------------------------------------------------------------------


#### everything()


All columns.


Usage

``` python
everything()
```


------------------------------------------------------------------------


#### features()


All feature columns regardless of type.


Usage

``` python
features()
```


------------------------------------------------------------------------


#### float()


All floating-point columns.


Usage

``` python
float()
```


------------------------------------------------------------------------


#### has_ancestor()


Columns with the given step **anywhere in their ancestry**.


Usage

``` python
has_ancestor(step=None, *, step_type=None)
```


The ancestral counterpart of [has_source](Selectors.md#mlfeatures.Selectors.has_source): matches columns even after they were further transformed (e.g. dummies that were then normalized). Takes the same arguments as [has_source](Selectors.md#mlfeatures.Selectors.has_source), including its `ValueError` for an id or type that names no earlier step.


------------------------------------------------------------------------


#### has_role()


All columns carrying `role`.


Usage

``` python
has_role(role)
```


Roles are those of the recipe at fit time, so a column that an earlier step dropped is not selected. A role that no column carries selects nothing.


------------------------------------------------------------------------


#### has_source()


Columns whose **immediate** producer is the given step.


Usage

``` python
has_source(step=None, *, step_type=None)
```


Pass either a step id or `step_type=` (a [Step](Step.md#mlfeatures.Step) subclass). Resolves against the lineage DAG at fit time. For columns that trace back to a step *anywhere* in their ancestry, use [has_ancestor](Selectors.md#mlfeatures.Selectors.has_ancestor).

A step's default id is its type's short name, and a second step of the same type in one recipe is given a suffixed id (`"dummy_2"`), so an id here names exactly one step.

Unlike a dtype or role selector, matching nothing is an error, not the identity transform: raises `ValueError` if no step earlier in the recipe has that id (or is an instance of that type). Pass `"original"` as the id for the input columns that survive to this point. Passing both `step` and `step_type`, or neither, also raises `ValueError`.


##### Examples

Normalize only the indicator columns that [StepDummy](StepDummy.md#mlfeatures.StepDummy) produced:


``` python
recipe = (
    Recipe()
    .add(StepDummy(s.string()))
    .add(StepNormalize(s.has_source(step_type=StepDummy)))
)
fitted = recipe.fit(df)
fitted.transform(df)
```


|     | x   | city_b    |
|-----|-----|-----------|
| 0   | 1.0 | -0.577350 |
| 1   | 2.0 | 1.154701  |
| 2   | 3.0 | -0.577350 |


------------------------------------------------------------------------


#### integer()


All integer columns (signed or unsigned).


Usage

``` python
integer()
```


------------------------------------------------------------------------


#### integer_features()


All feature columns that are integer-typed.


Usage

``` python
integer_features()
```


------------------------------------------------------------------------


#### matches()


Columns whose name matches the regular expression `pattern`.


Usage

``` python
matches(pattern)
```


Uses `re.search` and is case-sensitive (anchor explicitly with `^`/`$` if desired, and use `(?i)` to ignore case).


------------------------------------------------------------------------


#### nominal()


All nominal columns (string, categorical, or enum).


Usage

``` python
nominal()
```


------------------------------------------------------------------------


#### nominal_features()


All feature columns that are nominal (categorical, enum, or string).


Usage

``` python
nominal_features()
```


------------------------------------------------------------------------


#### num_range()


Columns named `f"{prefix}{i}"` for `i` in `rng` (e.g. x1..x5).


Usage

``` python
num_range(prefix, rng)
```


Names that aren't in the data frame are skipped, as with [col](Selectors.md#mlfeatures.Selectors.col). There is no zero-padding option (R's `width`); spell padded names out with [col](Selectors.md#mlfeatures.Selectors.col).


##### Examples


``` python
recipe = Recipe().add(StepNormalize(s.num_range("x", range(1, 3))))
fitted = recipe.fit(df)
fitted.transform(df)
```


|     | x1   | x2   | x3  |
|-----|------|------|-----|
| 0   | -1.0 | -1.0 | 7.0 |
| 1   | 0.0  | 0.0  | 8.0 |
| 2   | 1.0  | 1.0  | 9.0 |


------------------------------------------------------------------------


#### numeric()


All numeric columns: integer, floating-point, and decimal.


Usage

``` python
numeric()
```


Decimal columns only exist as such on polars; pandas stores them as `object`, which is not numeric.

This matches by dtype alone, so it also picks up the target and any ID column. In a step's `columns`, prefer [numeric_features](Selectors.md#mlfeatures.Selectors.numeric_features), which also requires the `feature` role.


------------------------------------------------------------------------


#### numeric_features()


All feature columns that are numeric.


Usage

``` python
numeric_features()
```


Unlike [numeric](Selectors.md#mlfeatures.Selectors.numeric), this requires the `feature` role, so a numeric target or case-weights column is left out. It is the one to use in a step's `columns` unless you really mean every numeric column. The other `*_features` selectors differ from their bare counterparts in the same way.


------------------------------------------------------------------------


#### ordered()


All ordered-categorical columns.


Usage

``` python
ordered()
```


narwhals reports a polars `Enum` and a pandas `Categorical` with `ordered=True` both as `Enum`, which is what this selects.


------------------------------------------------------------------------


#### starts_with()


Columns whose name starts with `prefix`.


Usage

``` python
starts_with(prefix)
```


Matching is case-sensitive and takes a single prefix. This differs from tidyselect, which ignores case by default and accepts several prefixes; combine selectors with `|` for the latter.


------------------------------------------------------------------------


#### string()


All string columns.


Usage

``` python
string()
```


------------------------------------------------------------------------


#### string_features()


All feature columns that are string-typed.


Usage

``` python
string_features()
```


------------------------------------------------------------------------


#### targets()


All target columns regardless of type.


Usage

``` python
targets()
```


------------------------------------------------------------------------


#### unordered()


All unordered-categorical columns.


Usage

``` python
unordered()
```


narwhals reports a polars `Categorical` and a pandas `Categorical` with `ordered=False` both as `Categorical`, which is what this selects.


------------------------------------------------------------------------


#### wrap()


Lift a raw name / list / polars / narwhals selector into mlfeatures-land.


Usage

``` python
wrap(selector)
```


Use this to let a polars or narwhals selector *lead* a composed expression, since `cs.numeric() & mlfeatures_sel` cannot work directly.


##### Examples


``` python
selector = s.wrap(cs.starts_with("x_")) & s.features()
recipe = Recipe().add(StepNormalize(selector))
fitted = recipe.fit(df)
fitted.transform(df)
```


|     | x_1  | x_2  | y   |
|-----|------|------|-----|
| 0   | -1.0 | -1.0 | 7.0 |
| 1   | 0.0  | 0.0  | 8.0 |
| 2   | 1.0  | 1.0  | 9.0 |
