Polars 2 cheatsheet

Polars 2: Lazy queries and files

7 common Polars 2 tasks.

Scan a file lazily

Build a query without loading the file. Polars optimizes it and reads only what it needs.

lazy = pl.scan_csv("big.csv")

Write and run a lazy query

Chain operations on a LazyFrame, then collect the result.

result = (
    pl.scan_csv("big.csv")
    .filter(pl.col("amount") > 100)
    .group_by("region")
    .agg(pl.col("amount").sum())
    .collect()
)

See the query plan

Print the optimized plan of a lazy query.

print(lazy.filter(pl.col("amount") > 100).explain())

Read and write Parquet

Parquet is compact and fast to read.

df.write_parquet("data.parquet")
df = pl.read_parquet("data.parquet")

Stack DataFrames

Append DataFrames that have the same columns.

pl.concat([df, df])

Convert to and from pandas

Convert for libraries that expect pandas. This needs pyarrow installed.

pandas_df = df.to_pandas()
df = pl.from_pandas(pandas_df)

Process data larger than memory

Collect with the streaming engine so data is handled in batches.

pl.scan_csv("huge.csv").group_by("id").len().collect(engine="streaming")

Was this page helpful?