Polars Utilities¶
mo_polars_kit provides frame-level utilities for transformation, normalization, and null handling in Polars-based API workflows. If you are preparing frames for bulk writes, review Data Operations (CRUD).
Stability Warning
`mo_polars_kit` is not part of the core offering and is currently experimental and subject to change without any prior notice. Refer to this page to keep informed on development with Polars utilities.
Implementation¶
Start by importing the polars kit in your API or utility module:
1. Is Frm Empty¶
Check whether a Polars frame has zero rows.
Usage:
Parameters:
frm(pl.DataFrame | pl.LazyFrame): Frame to inspect.
Possible responses:
- Returns
Truewhen frame has no rows. - Returns
Falsewhen at least one row exists.
2. Is Model Frms Empty¶
Check whether every frame in a model-frame map is empty.
Usage:
Parameters:
model_frms(dict[Any, pl.DataFrame|pl.LazyFrame]): Model-frame mapping.
Possible responses:
- Returns
Truewhen all mapped frames are empty. - Returns
Falsewhen any mapped frame contains rows.
3. Is Model Frms Not Empty¶
Check whether at least one frame in a model-frame map is not empty.
Usage:
Parameters:
model_frms(dict[Any, pl.DataFrame|pl.LazyFrame]): Model-frame mapping.
Possible responses:
- Returns
Truewhen at least one frame has rows. - Returns
Falsewhen all frames are empty.
4. Collect Model Frms¶
Collect all lazy frames in a model-frame map into eager DataFrames.
Usage:
Parameters:
df_dict(dict[Any, pl.DataFrame|pl.LazyFrame]): Input model-frame mapping.streaming(bool, default=True): Uses streaming collection for LazyFrame inputs.
Possible responses:
- Returns
dict[Any, pl.DataFrame]with all values materialized as DataFrame.
5. Sync Model Frms Type¶
Normalize model-frame map to a single Polars execution type.
Usage:
Parameters:
model_frms(dict[Any, pl.DataFrame|pl.LazyFrame]): Input mapping.
Behavior:
- If any value is
LazyFrame, allDataFramevalues are converted toLazyFrame. - If all values are
DataFrame, mapping is returned unchanged.
Possible responses:
- Returns normalized model-frame mapping.
6. Has Nulls In Frm Col¶
Check whether a frame column contains null values.
Usage:
Parameters:
frm(pl.DataFrame | pl.LazyFrame): Frame to inspect.column(str): Target column name.
Possible responses:
- Returns
Truewhen at least one null is present. - Returns
Falsewhen no nulls exist.
7. Split Model Frms On Null¶
Split model-frame map into valid/invalid partitions by nullability of an error column.
Usage:
Parameters:
model_frms(dict[Any, pl.DataFrame|pl.LazyFrame]): Model-frame mapping.column(str, default="__error__info"): Error/status column used for split.
Possible responses:
- Returns
(valid_model_frms, invalid_model_frms). - Valid partition contains rows where
columnis null. - Invalid partition contains rows where
columnis not null.
8. Frm Fill Null¶
Fill null values in a column using literal or callable modes.
Usage:
Parameters:
fr(pl.DataFrame | pl.LazyFrame): Target frame.column(str): Column to update.fill_value(Any | Callable): Literal value or callable for generated values.mode("lit" | "map" | "sink_map"): Fill strategy.dtype(type | None, default=None): Target dtype for generated values.**custom_params: Additional kwargs passed to callable fill function.
Varieties:
lit: direct literal fill.map: in-memory batch mapping.sink_map: lazy sink to parquet then scan back for large lazy pipelines.
Possible responses:
- Returns transformed
DataFrame/LazyFramewith nulls filled.
9. Frm Fill Notnull¶
Transform non-null values in a column using literal or callable modes.
Usage:
frm = mo_polars_kit.frm_fill_notnull(
frm,
column="email",
fill_value=lambda row: row.lower(),
mode="map",
row_param="row",
dtype=pl.Utf8,
)
Parameters:
fr(pl.DataFrame | pl.LazyFrame): Target frame.column(str): Column to update.fill_value(Any | Callable): Literal value or callable transform.mode("lit" | "map" | "sink_map"): Transformation strategy.row_param(str | None, default=None): Callable kwarg name for current non-null value.dtype(type | None, default=None): Target dtype.**custom_params: Extra kwargs for callable transform.
Varieties:
lit: replace all non-null values with one literal.map: apply callable across batches.sink_map: lazy sink-based map flow for large lazy frames.
Possible responses:
- Returns transformed
DataFrame/LazyFramewith non-null values updated.
10. Get Frm Height¶
Get frame row count for eager or lazy Polars inputs.
Usage:
Parameters:
frm(pl.DataFrame | pl.LazyFrame): Target frame.
Possible responses:
- Returns row count as
int.
Example Usage¶
import polars as pl
from django_mindoff.components.polars_kit import mo_polars_kit
frm = pl.DataFrame({
"email": ["A@EXAMPLE.COM", None],
"status": [None, "active"],
})
normalized = mo_polars_kit.frm_fill_notnull(
frm,
column="email",
fill_value=lambda row: row.lower(),
mode="map",
row_param="row",
dtype=pl.Utf8,
)
filled = mo_polars_kit.frm_fill_null(
normalized,
column="status",
fill_value="draft",
mode="lit",
)
Troubleshooting¶
frm_fill_nullandfrm_fill_notnullsupportlit,map, andsink_mapmodes.- Use
sink_mapfor lazy/streaming pipelines where full in-memory materialization is not desired.