Skip to content

engine

Batched lazy execution of column summaries.

Every statistic in a profile is a Polars expression or a small LazyFrame query. Running them column by column would scan the data once per column and per step. Instead each summary step is written as a generator that yields a :class:Request (the scalar aggregations and LazyFrame queries it needs) and receives a :class:Response with the results::

def describe_numeric(config, column, summary):
    response = yield Request(aggs={"mean": column.expr.mean()})
    summary["mean"] = response.values["mean"]

:func:run_plans advances the generators of all columns in lock step. In every round it gathers the requests of every column that is waiting, evaluates all scalar aggregations in a single LazyFrame.select and all queries in a single polars.collect_all call (which shares common sub-plans such as the scan of the source), and sends each generator its own slice of the results. A profile of k columns therefore costs a handful of passes over the data instead of k times that.

Plain (non-generator) functions are accepted wherever a step is expected, so a custom summarizer can still be written as an ordinary function that materialises what it needs with :meth:ColumnRef.series.

Classes

Request dataclass

What a summary step needs evaluated.

Attributes: aggs: named expressions evaluated together over the profiled frame. Each must reduce to a single value (an aggregation). frames: named LazyFrames, collected together.

Response dataclass

Results of a :class:Request, keyed like the request.

ColumnRef dataclass

A column of the profiled frame, as summary steps see it.

Attributes: name: the column name. frame: the typed LazyFrame (columns cast to their inferred type). dtype: the Polars dtype of the column in frame. source_dtype: the dtype of the column in the input data.

Attributes

expr property

The column as an expression on :attr:frame.

Methods:

lazy()

A single-column LazyFrame.

series()

Materialise the column (for steps that cannot stay lazy).

Functions:

run_plans(frame, plans, on_done=None)

Drive several plans to completion, batching their requests.

Args: frame: the LazyFrame the scalar aggregations are evaluated on. plans: plans keyed by an identifier. A value that is not a generator is taken as an already-computed result. on_done: called with the key of each plan as it finishes.

Returns: The return value of every plan, by key.

chain(*steps)

Run plans one after the other as a single plan.