ColumnSelector¶
Reference¶
class polars_learn.ColumnSelector(
columns,
*,
rename=None,
convert_pandas=False,
warn_on_conversion=True,
verbosity=0,
)Select and optionally rename columns in a Polars DataFrame or LazyFrame.
Use this transformer to define the exact feature names and column order
passed to a model. fit checks selected names and records the layout;
transform selects those columns in the supplied order, applies optional
renames, and checks the output names against fitting. Other columns are
omitted. Values are unchanged and dtypes are not validated or cast.
Choose FeaturePassthrough when renaming is unnecessary, or add a schema
validator when input dtypes must be enforced.
Polars is the execution engine. Native DataFrames and LazyFrames are
accepted; pandas input requires convert_pandas=True and is converted
behind the scenes. Output is native Polars by default, including an eager
Polars frame for converted pandas input. Use
set_output(transform="pandas") for compatibility output; install
polars-learn[pandas] for pandas and PyArrow. Lazy output defers row
execution, while eager Polars or pandas output collects during transform.
Prefer native Polars input/output for speed and to avoid conversion overhead.
Parameters¶
columnssequence of strNonempty ordered sequence of unique, nonempty input column names to keep.
renamedict of str to str, default=NoneOptional mapping from selected source names to nonempty output names. Final output names, including unchanged names, must remain unique.
convert_pandasbool, default=FalseAllow pandas DataFrames and convert them to Polars. Requires pandas; some dtypes also require PyArrow.
verbosity{0, 1, 2}, default=0Print progress at 1 and schema/operation details at 2; 0 is silent.
warn_on_conversionbool, default=TrueEmit
PandasConversionWarningwhen converting pandas input.
Attributes¶
feature_names_in_ndarray of str, dtype=objectAll training input column names in input order, including unselected names.
n_features_in_intNumber of all training input columns.
columns_tuple of strSelected source names recorded by fit.
output_features_ndarray of str, dtype=objectSelected output names after renaming, in selection order.
Raises¶
TypeErrorIf columns, rename, or common options have invalid types.
ValueErrorIf selected or final renamed names are duplicated, rename keys are unselected, or verbosity is outside 0, 1, and 2.
See also¶
SchemaSpec: Validate explicit required dtypes before selecting columns. FeaturePassthrough: Explicit unchanged-column selection without renaming.
Notes¶
DataFrames return DataFrames and LazyFrames return unevaluated LazyFrames by default. Use set_output to force eager, lazy, or pandas output.
Examples¶
>>> import polars as pl
>>> from polars_learn import ColumnSelector
>>> X = pl.DataFrame({"a": [1], "b": [2]})
>>> selector = ColumnSelector(["b"], rename={"b": "value"})
>>> selector.fit_transform(X.lazy()).collect().columns
['value']
Methods¶
| Method | Purpose |
|---|---|
| fit(X, y=None) | Check selected columns and record input and output names. |
| transform(X) | Select fitted columns, rename them, and verify output names. |
| fit_transform(X, y=None, **fit_params) | Fit and transform the same input using the configured return kind. |
| get_feature_names_out(input_features=None) | Return the fitted output column names. |
| get_params(deep=True) | Get estimator constructor parameters. |
| set_params(**params) | Set constructor parameters using scikit-learn conventions. |
| set_output(*, transform=None) | Configure native Polars output or pandas compatibility output. |
fit¶
fit(X, y=None)Check selected columns and record input and output names.
Call before transforming to establish the selected output layout. Checks
that all configured columns exist and records all input names plus the
selected output names, including any renames. Extra input columns are
allowed, input order may differ, and no training statistics are learned.
This inherited method also fits FeaturePassthrough, which has no renames.
Parameters¶
Xpolars.DataFrame, polars.LazyFrame or pandas.DataFrameInput containing every selected column. Other columns and a different input order are allowed. Pandas requires conversion enabled.
yobject, default=NoneIgnored; accepted for scikit-learn compatibility.
Returns¶
selfColumnSelector or FeaturePassthroughThe fitted selection transformer. Dtypes are not learned or validated.
Raises¶
TypeErrorIf input, options, or column/rename parameter types are unsupported.
ValueErrorIf configuration is invalid or selected columns are missing.
Notes¶
Lazy input is checked through schema metadata without collecting rows. Output configuration does not change the return from fit.
transform¶
transform(X)Select fitted columns, rename them, and verify output names.
Call after fitting to prepare the selected model inputs. Selects columns
in configured order, optionally renames them, and checks the resulting
names against the fitted layout. Unselected columns are omitted; dtype
changes are accepted. For FeaturePassthrough, selection is unchanged
and no renames occur. The original input is never modified.
Output follows set_output: native Polars by default, eager Polars for
"polars", a LazyFrame for "polars-lazy", or a pandas DataFrame for
"pandas". Eager Polars and pandas output collect lazy rows. Pandas output
uses Polars conversion defaults and may change dtype/null representation;
missing pandas or PyArrow raises ImportError at conversion. Pandas input
requires convert_pandas=True independently of the output choice.
Parameters¶
Xpolars.DataFrame, polars.LazyFrame or pandas.DataFrameInput containing the selected fitted columns. Unselected columns are ignored; input order and dtypes may differ from fitting. Pandas requires conversion enabled.
Returns¶
X_selectedpolars.DataFrame, polars.LazyFrame or pandas.DataFrameSelected columns in configured order, including renames. Default preserves input kind; set_output can force eager, lazy, or pandas output.
Raises¶
sklearn.exceptions.NotFittedErrorIf fit has not completed successfully.
TypeErrorIf input or parameter types are unsupported.
ValueErrorIf selected columns are missing, configuration is invalid, or renamed output names differ from fitting.
Notes¶
Lazy metadata errors occur now; source/value errors can occur when a returned plan is collected. Explicit eager output collects within this method. Source and collection errors are not wrapped.
fit_transform¶
fit_transform(
X,
y=None,
**fit_params,
)Fit and transform the same input using the configured return kind.
Convenience method for preparing training data: calls fit(X, y) and
then transform(X) on the same input. This updates fitted state and
returns the prepared frame. The target is ignored and extra fit parameters
are unsupported. See fit for the class-specific learned contract and
transform for validation and output behavior.
Output follows set_output: native Polars by default, eager Polars for
"polars", a LazyFrame for "polars-lazy", or a pandas DataFrame for
"pandas". Eager Polars and pandas output collect lazy rows. Pandas output
uses Polars conversion defaults and may change dtype/null representation;
missing pandas or PyArrow raises ImportError at conversion. Pandas input
requires convert_pandas=True independently of the output choice.
Parameters¶
Xpolars.DataFrame, polars.LazyFrame or pandas.DataFrameInput accepted by fit and transform. Pandas requires conversion enabled.
yobject, default=NoneIgnored; accepted for scikit-learn compatibility.
**fit_paramsdictExtra fit parameters are unsupported and raise TypeError.
Returns¶
X_transformedpolars.DataFrame, polars.LazyFrame or pandas.DataFrameTransformed input. Default preserves input kind; set_output can force an eager DataFrame or LazyFrame. Converted pandas is eager.
See also¶
fit: Validate input and record fitted state.
transform: Apply the fitted transformation and output configuration.
get_feature_names_out¶
get_feature_names_out(input_features=None)Return the fitted output column names.
Use these names to label model inputs or inspect a fitted pipeline. Returns a copy of the names recorded during fitting; it does not transform data, collect lazy rows, or refit the transformer. Supplied input names are checked against all fitted input names in exact order. For SchemaValidator, inference-only extra columns are not included; this is the fitted contract rather than a live query's column list.
Parameters¶
input_featuresarray-like of str or None, default=NoneIf supplied, names must exactly match
feature_names_in_.
Returns¶
feature_names_outndarray of shape (n_features_out_,), dtype=objectCopy of fitted output names. SchemaValidator excludes inference-only extras. SchemaSpec includes preserved training extras. A selector lists selected names, including any renames; a passthrough lists unchanged selected names. An engineer lists all output names.
Raises¶
sklearn.exceptions.NotFittedErrorIf the transformer has not been fitted.
ValueErrorIf supplied input names differ from fitted names or order.
get_params¶
get_params(deep=True)Get estimator constructor parameters.
Parameters¶
deepbool, default=TrueInclude nested estimator parameters when present.
Returns¶
paramsdictConstructor parameters and, when requested, nested parameters. Output configuration is managed separately by set_output.
set_params¶
set_params(**params)Set constructor parameters using scikit-learn conventions.
Parameters¶
**paramsdictConstructor parameter names and values. Nested estimator parameters use the step__parameter convention where supported.
Returns¶
selfestimator instanceThis estimator with updated configuration.
Raises¶
ValueErrorIf a parameter name is unknown.
Notes¶
Changing parameters does not immediately validate configuration or clear fitted attributes. Refit after contract changes. Invalid parameter values fail at the next relevant fit or transform operation.
set_output¶
set_output(*, transform=None)Configure native Polars output or pandas compatibility output.
Call this when a downstream consumer needs a particular frame container. It only stores a choice for subsequent transform/fit_transform calls; it does not fit, transform, or collect data at configuration time. Use "default" to follow each call's input kind, "polars" for eager Polars, "polars-lazy" for a lazy plan, or "pandas" for an eager pandas DataFrame. Converted pandas input is treated as eager Polars by default. Pandas input permission remains separate: enable convert_pandas on any transformer receiving pandas, even when pandas output is requested.
Parameters¶
transform{"default", "polars", "polars-lazy", "pandas"} or None, default=NoneDefault preserves the input kind. "polars" collects an eager DataFrame; "polars-lazy" returns a LazyFrame.
Noneleaves the configuration unchanged. "pandas" collects if necessary and converts the result to a pandas DataFrame. Requires the pandas extra.
Returns¶
selfestimator instanceThis transformer.
Raises¶
ValueErrorIf an unsupported output container is requested.
Notes¶
Prefer native Polars output for speed and to avoid conversion overhead. Pandas output is provided for compatibility and uses Polars to_pandas defaults, including a fresh RangeIndex and dtype/null conversion. Conversion raises ImportError if pandas or PyArrow is unavailable. Input conversion remains separately controlled by convert_pandas. Automatic scikit-learn output wrapping and global output settings are bypassed to preserve native names and permitted extra columns. Output choices survive cloning. None leaves the current choice intact; "default" resets input-kind preservation. Fit always returns self and does not collect lazy rows. Forcing lazy output from eager input may wrap an already evaluated result; use lazy input to defer evaluation.
See also¶
Shared estimator conventions · Production guidance · Persistence