PDS provides a Pipeline and Blueprint system for creating reproducible data transformation workflows.
- Initialize a Blueprint: Use
Blueprint(df, name=..., target=..., lowercase=True) to define the starting state and the target column. The target column is automatically excluded from certain operations like scaling. - Define Transformations: Chain methods such as
.filter(), .linear_impute(), .impute(), .scale(), .one_hot_encode(), .woe_encode(), and .target_encode(). - Append Expressions: Use
.append_expr() to add custom Polars expressions (e.g., log transforms, clipping, or missing value flags) to the pipeline. - Materialize and Transform: Call
.materialize() on the Blueprint to get a Pipeline object, then use .transform(df) to apply the sequence to your data.
import polars as pl
import polars.selectors as cs
from polars_ds.pipeline import Pipeline, Blueprint
bp = (
Blueprint(df, name = "example", target = "approved", lowercase=True)
.filter(pl.col("city_category").is_not_null())
.linear_impute(features = ["var1", "existing_emi"], target = "loan_period")
.impute(["existing_emi"], method = "median")
.append_expr(
pl.col("existing_emi").log1p().alias("existing_emi_log1p"),
pl.col("loan_amount").log1p().alias("loan_amount_log1p"),
pl.col("loan_amount").clip(lower_bound = 0, upper_bound = 1000).alias("loan_amount_clipped"),
pl.col("loan_amount").sqrt().alias("loan_amount_sqrt"),
pl.col("loan_amount").shift(-1).alias("loan_amount_lead_1")
)
.scale(
cs.numeric().exclude(["var1", "existing_emi_log1p"]), method = "standard"
)
.append_expr(
pl.col("employer_category1").is_null().cast(pl.UInt8).alias("employer_category1_is_missing")
)
.one_hot_encode("gender", drop_first=True)
.woe_encode("city_category")
.target_encode("employer_category1", min_samples_leaf = 20, smoothing = 10.0)
)
pipe:Pipeline = bp.materialize()
df_transformed = pipe.transform(df)