Skip to main content

Step.resolve_selection()

Resolve this step’s columns selector to concrete column names.

Usage

Source

Step.resolve_selection(
    data,
    info,
)

This is the supported way for a step to find out which columns it was pointed at. Call it at the top of fit() and select from the data frame with the names it returns; narwhals’ own frame.schema then gives the dtypes:

def fit(self, training_data, info):
    names = self.resolve_selection(training_data, info)
    selected = training_data.select(names)
    dtypes = {name: str(dt) for name, dt in selected.schema.items()}
    ...

The default resolves self.columns, which is right for every selector-taking step, including ones that take a second selector for a different purpose: the primary input selector is always columns. Override only if a step’s primary input is named something else.

Takes data as well as info because narwhals and polars selectors resolve against the data frame’s schema rather than against the schema snapshot alone.

The recipe also calls this before fit() so that a selection matching no columns makes the step the identity transform; a step that resolves its own selection therefore never has to handle the empty case itself.