Skip to main content

Schema

A structural snapshot of the training data used for boundary checks.

Usage

Source

Schema(
    dtypes,
    roles,
    transform_required,
)

Captured at Recipe.fit() and exposed as FittedRecipe.schema. Holds the training narwhals.Schema (column names and dtypes), the role assignments, and a precomputed per-column flag for whether each column is required at transform() time. Not to be confused with ColSchema (what a step reports about the columns it consumed and produced) or SchemaInfo (what a step is handed at fit time).

Dtypes are compared exactly: a column that was Float64 at fit must be Float64 in new data, and parametrized dtypes must agree on their parameters (a Datetime in microseconds does not match one in nanoseconds, nor does an Enum with different categories). Cast new data to the training dtypes before transforming.

Attributes

dtypes: nw.Schema

The training data frame’s column dtypes, as a narwhals schema: an ordered name→dtype mapping.

roles: Roles

Role assignments of the input columns, as the data entered the recipe (after any role operation written before the first step). Role operations written after a step are not reflected here.

transform_required: dict[str, bool]
Per-column flag: True if the column must be present in new data at transform() time. Computed at fit from role requirements and any update_role_requirements overrides. Read this rather than deriving the requirement from roles: a role operation written after a step can change a column’s requirement without changing the roles recorded here.

Examples

The schema is built by the recipe; read it back from a fitted recipe:

import pandas as pd
from mlfeatures import Recipe, StepNormalize
from mlfeatures import Selectors as s

df = pd.DataFrame({"x": [1.0, 2.0, 3.0], "y": [0, 1, 0]})
rec = Recipe(target="y", features="x").add(
    StepNormalize(s.numeric_features())
)
rec = rec.fit(df)
rec.schema.dtypes.names()
['x', 'y']