ValueValidator¶
ValueValidator applies named row-level Boolean expressions to catch nulls,
bounds, and allowed-value violations before data reaches model features. Add it
after a schema component when both structural and value contracts matter. See
the value validation guide for composition
and lazy execution details.
Reference¶
class polars_learn.ValueValidator(
checks,
*,
convert_pandas=False,
warn_on_conversion=True,
verbosity=0,
)Reject rows that fail named Boolean Polars expressions.
Use this transformer after structural schema validation for row-level rules such as non-null requirements, bounds, and allowed-value checks. Every check must produce one Boolean value per row. False and null results are violations. The input is returned unchanged when all checks pass.
Parameters¶
checksdict of str to polars.ExprNonempty mapping from diagnostic labels to row-wise Boolean expressions. For example,
{"positive_price": pl.col("price") > 0}.convert_pandasbool, default=FalseAllow pandas DataFrames and convert them to Polars.
warn_on_conversionbool, default=TrueEmit
PandasConversionWarningwhen converting pandas input.verbosity{0, 1, 2}, default=0Print progress at 1 and schema/check details at 2; 0 is silent.
Attributes¶
feature_names_in_ndarray of str, dtype=objectInput column names and order recorded during fit.
n_features_in_intNumber of input columns recorded during fit.
output_features_ndarray of str, dtype=objectInput column names, unchanged by validation.
Raises¶
TypeErrorIf checks, expressions, or common options have invalid types.
ValueErrorIf checks are empty, labels are invalid, or expressions do not resolve to Boolean values on the fitted schema.
ValueValidationErrorIf an input schema differs from fitting, checks cannot execute, or any check returns false or null for one or more rows.
sklearn.exceptions.NotFittedErrorIf transform is called before a successful fit.
Notes¶
fit resolves the check expressions against schema metadata only; it never
evaluates row values. transform evaluates the checks once. Lazy inputs
are materialized during transform so rule violations fail before it returns
and default output can preserve the lazy container using an in-memory frame.
Value checks do not add columns, mutate input, or change row order.
Examples¶
>>> import polars as pl
>>> from polars_learn import ValueValidator
>>> frame = pl.DataFrame({"price": [2.0, 3.0], "quantity": [1, 2]})
>>> checks = {
... "positive_price": pl.col("price") > 0,
... "nonnegative_quantity": pl.col("quantity") >= 0,
... }
>>> validator = ValueValidator(checks)
>>> validator.fit_transform(frame).equals(frame)
True
Methods¶
| Method | Purpose |
|---|---|
| fit(X, y=None) | Resolve checks against the input schema without evaluating rows. |
| transform(X) | Validate fitted input names, order, dtypes, and configured values. |
| fit_transform(X, y=None, **fit_params) | Fit the schema contract, validate values, and return the input. |
| 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)Resolve checks against the input schema without evaluating rows.
transform¶
transform(X)Validate fitted input names, order, dtypes, and configured values.
fit_transform¶
fit_transform(
X,
y=None,
**fit_params,
)Fit the schema contract, validate values, and return the input.
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