FeatureSpec¶
Reference¶
class polars_learn.FeatureSpec(
expression,
*,
alias=None,
drop=None,
name=None,
verbosity=0,
)Describe a native Polars feature operation.
Use this class to describe a reusable feature calculation for
FeatureEngineer. Construction validates and stores the expressions;
it does not execute them, fit on data, or implement transform.
The engineer applies the expressions with Polars with_columns, then
removes the configured drop columns. Existing columns can be replaced.
Expressions within one specification read the same stage's input, so they
cannot read columns newly created by siblings. Compose expressions directly
from existing columns, or use later specifications for dependent features.
The engineer applies specifications in the order you supply them.
Parameters¶
expressionpolars.Expr or sequence of polars.ExprSingle expression or nonempty sequence evaluated together, normalized to a tuple. Native output names are used unless
aliasis set. Sibling expressions cannot depend on outputs created within the same specification.aliasstr or None, default=NoneOverride the output name of a single expression. Multiple expressions or an expression expanding to multiple outputs cannot use this option.
dropsequence of str or None, default=NoneColumns to remove after evaluating the expressions. Missing names fail during execution; later specifications cannot use dropped columns.
verbosity{0, 1, 2}, default=0Print validation summaries at 1 and output/drop details at 2.
namestr or None, default=NoneDescriptive label included in feature execution errors.
Attributes¶
expressiontuple of polars.ExprCanonical expression representation, including any explicit alias.
droptuple of strColumns to drop after evaluating this specification.
namestr or NoneOptional debugging label.
verbosityintConfigured diagnostic level, independent of the feature engineer.
Raises¶
TypeErrorIf expressions, drop columns, containers, or verbosity have invalid types.
ValueErrorIf the expression sequence is empty, an explicit alias is ambiguous, output names are duplicated, a label is invalid, or verbosity is outside the supported levels.
See also¶
FeatureEngineer: Execute an ordered sequence of specifications.
Notes¶
Existing columns may be overwritten deliberately. Expressions follow native Polars naming, so an additional alias is not required when the expression already has a determinable output name. Selector names and duplicates that depend on the input schema are resolved by the feature engineer. Expressions may contain map_elements or map_batches Python callbacks. Declare return_dtype for predictable schema checks and use pure functions with serializable state. Prefer native expressions for speed; callbacks and their dependencies must remain usable in the deployment environment. This class is an operation description, not a scikit-learn estimator. Its verbosity reports construction independently of an engineer's verbosity. Pickling preserves native expressions using Polars serialization, including Python callbacks with serializable state. Version mismatches emit InconsistentVersionWarning before state restoration. Compatible cross-version expressions are attempted using JSON; incompatible state raises SerializationError. Load trusted artifacts only and validate after upgrading.
Examples¶
>>> import polars as pl
>>> from polars_learn import FeatureSpec
>>> spec = FeatureSpec(pl.col("price") * pl.col("quantity"), alias="notional")
>>> spec.expression[0].meta.output_name()
'notional'
Methods¶
FeatureSpec is a description, not an estimator. It has no fit, transform, or prediction methods. Pass it to FeatureEngineer for execution.
See also¶
Shared estimator conventions · Production guidance · Persistence