Materialize a finished query
Turn a composed lazy transformation pipeline into a DataFrame for inspection or downstream Python code.
Polars · Module
Materializes a LazyFrame's deferred query plan into a DataFrame. You can select the execution engine, run in the background, and configure query optimizations.
A LazyFrame records a query plan without immediately executing it. Calling collect materializes that plan into a DataFrame, choosing the execution engine and optimization settings through keyword arguments.
LazyFrame.collect(*, engine: EngineType = 'auto', background: bool = False, optimizations: QueryOptFlags = (), **_kwargs: Any) → DataFrame | InProcessQuery| Name | Type | Description |
|---|---|---|
engine | EngineType = 'auto' | Selects the execution engine; supported values include auto, in-memory, streaming, and gpu. |
background | bool = False | Runs collection in the background and returns an in-process query handle. |
optimizations | QueryOptFlags = () | Configures the query optimization flags used during collection. |
**_kwargs | Any | Accepts additional keyword arguments. |
DataFrame | InProcessQuery
import polars as pl
lf = pl.LazyFrame({"group": ["a", "b", "a"], "value": [1, 2, 3]})
result = (
lf.group_by("group")
.agg(pl.col("value").sum())
.collect(engine="in-memory")
)
print(result)Builds a lazy aggregation plan and materializes it as an in-memory DataFrame.
Turn a composed lazy transformation pipeline into a DataFrame for inspection or downstream Python code.
Process a dataset in batches when an in-memory execution plan would create excessive memory pressure.
Run supported lazy computations on CUDA hardware with the required Polars GPU integration installed.
Submit collection without blocking the current control flow when background execution is appropriate.
collect executes a LazyFrame's deferred query plan and returns a DataFrame. Its default engine setting is auto, which selects the configured or default execution engine.
Follow-up: How would you reduce memory pressure for a large query? Use the streaming engine, which processes queries in batches.
The engine parameter selects execution: auto, in-memory, streaming, or GPU when the required CUDA and cudf-polars support is available. The streaming engine processes data in batches.
Follow-up: What prerequisite is required for GPU execution? The environment needs an Nvidia GPU and cudf-polars.
collect accepts an optimizations setting that controls query optimization behavior. Individual optimization flags can be configured through QueryOptFlags rather than changing the lazy plan itself.
Follow-up: How can multiple lazy queries be collected together? Use polars.collect_all, which can combine computation graphs and apply common subplan elimination.
polars.collect_allpolars.LazyFrame.collect_asyncpolars.LazyFrame.explainpolars.LazyFrame.profile