Polars
Blazingly fast DataFrame library for Python with a lazy expression API, multi-threading, and Rust core.
Official Documentation ↗v1. Check the official docs for the latest API surface.Installation
pip install polarsMethods
No methods match this filter.
Blazingly fast DataFrame library for Python with a lazy expression API, multi-threading, and Rust core.
Overview
Polars is a DataFrame engine with a Rust core and an expression-first API. Expressions like pl.col(“amount”).sum() are plans, not immediate computations, so a chain of filters, joins, and aggregations compiles into one optimized, multi-threaded execution.
Lazy frames add query optimization over CSV, Parquet, and IPC sources: predicate and projection pushdown mean unneeded data is never read. The eager DataFrame API mirrors the same expressions for interactive work, and arrow-backed interop keeps exchange with pandas and databases cheap.
Core Methods
The most frequently used entry points, with signatures and runnable examples:
read_csv / scan_csv— Eagerly loads a CSV into memory, or lazily defers reading until collect optimizes the full plan.filter / select / with_columns— Row filtering and column projection or extension using expressions that evaluate in parallel.group_by / agg— Groups rows by keys and aggregates with expressions such as sum, mean, and count per group.join— Joins two frames on keys with standard strategies; joining on expressions avoids alignment steps.
Usage Notes
- Prefer scan_csv or scan_parquet with a final collect for pipelines; eager reads are for exploration.
- Build logic from expressions and window functions before falling back to row-wise apply.
- Write Parquet with compression for intermediate datasets; repeated CSV parsing dominates many naive pipelines.
Common Pitfalls
- applying Python functions per row silently serializes execution and erases the speed advantage.
- Chaining eager DataFrames defeats pushdown: each step materializes the full frame.
- String columns that look numeric stay strings; cast explicitly with .cast(pl.Int64) before arithmetic.
FAQ
- When does lazy mode pay off?
- scan_csv plus a chain of filter, select, and group_by lets Polars push projections and predicates into the reading step, skipping unneeded columns and rows. For files larger than memory or wide CSVs, lazy mode is dramatically cheaper than eager loading.
- How do I move data between Polars and pandas?
- df.to_pandas() converts eagerly, and pl.from_pandas(pdf) comes back. Conversions copy unless arrow-backed, so do transformations on the Polars side and convert only at integration boundaries.
- Why are expressions better than apply?
- Expressions describe intent, and the engine runs them vectorized across threads. map_batches or apply falls back to row-wise Python, forfeiting parallelism; reach for it only for logic that truly cannot be expressed.
Report a problem with this API docOpens a pre-filled issue on GitHub.