Build optimized pipelines
Compose filters, projections, joins, and aggregations before execution so Polars can optimize the complete plan.
Polars · Module
Starts a lazy query from an existing DataFrame and returns a LazyFrame. Operations remain deferred until Polars executes or inspects the plan.
Calling df.lazy() converts the existing DataFrame into a LazyFrame query plan. Subsequent lazy operations are deferred, allowing Polars to optimize and parallelize the query before a trigger such as collect(), explain(), show_graph(), or collect_schema() executes or inspects it.
DataFrame.lazy() → LazyFrameLazyFrame
import polars as pl
df = pl.DataFrame({"x": [1, 2, 3]})
result = (
df.lazy()
.filter(pl.col("x") > 1)
.collect()
)
print(result)Builds a lazy filter query and executes it with collect().
Compose filters, projections, joins, and aggregations before execution so Polars can optimize the complete plan.
Inspect execution behavior before running a potentially expensive query.
Keep query construction separate from execution in reusable data-processing functions.
df.lazy() returns a LazyFrame that represents a deferred query plan rather than immediately producing a result. The plan runs when a trigger such as collect() is called.
Follow-up: How can the plan be inspected without materializing the result? Call explain(), or use show_graph() for a Graphviz representation.
Lazy execution lets Polars optimize the complete query and can provide additional parallelism before execution. This is why lazy operations are recommended in the documentation.
Follow-up: Which trigger returns the final schema without collecting the data? Use collect_schema().
The lazy plan can be inspected or triggered with collect(), explain(), show_graph(), or collect_schema(). collect() runs the query on all data, while the other methods inspect or derive planning information.
Follow-up: What issue can arise when collecting several branches from one LazyFrame? Each branch may re-evaluate shared work; pl.collect_all can execute related plans together.
polars.LazyFramepolars.LazyFrame.collectpolars.LazyFrame.explainpolars.LazyFrame.show_graphpolars.LazyFrame.collect_schema