| name | polars-expertise |
| description | This skill should be used when the user asks about Polars DataFrame library (Apache Arrow) for Python or Rust. Triggers: "polars expressions", "lazy vs eager", "scan_parquet streaming", "convert pandas to polars", "pyspark to polars", "kdb to polars", "group_by_dynamic", "rolling_mean", "polars window functions", "asof join", "polars GPU", "polars parquet", "LazyFrame". Time series: OHLCV resampling, rolling windows, financial data patterns. Performance: native expressions over map_elements, early projection, categorical types, streaming.
|
Polars
High-performance DataFrame library built on Apache Arrow. Supports Python and Rust with expression-based API, lazy evaluation, and automatic parallelization.
Quick Start
Python
uv pip install polars
import polars as pl
df = pl.DataFrame({"symbol": ["AAPL", "GOOG"], "price": [150.0, 140.0]})
df.filter(pl.col("price") > 145).select("symbol", "price")
lf = pl.scan_parquet("trades.parquet")
result = lf.filter(pl.col("volume") > 1000).group_by("symbol").agg(
pl.col("price").mean().alias("avg_price")
).collect()
Rust
[dependencies]
polars = { version = "0.46", features = ["lazy", "parquet", "temporal"] }
use polars::prelude::*;
fn main() -> PolarsResult<()> {
let df = df![
"symbol" => ["AAPL", "GOOG"],
"price" => [150.0, 140.0]
]?;
let lf = LazyFrame::scan_parquet("trades.parquet", Default::default())?;
let result = lf
.filter(col("volume").gt(lit(1000)))
.group_by([col("symbol")])
.agg([col("price").mean().alias("avg_price")])
.collect()?;
Ok(())
}
Core Pattern: Expressions
Everything in Polars is an expression. Expressions are composable, lazy, and parallelized.
pl.col("price")
pl.col("price") * pl.col("volume")
pl.col("price").mean().over("symbol")
pl.when(cond).then(a).otherwise(b)
Expressions execute in contexts: select(), with_columns(), filter(), group_by().agg()
When to Use Lazy
Use Lazy (scan_*, .lazy()) | Use Eager (read_*) |
|---|
| Large files (> RAM) | Small data, exploration |
| Complex pipelines | Simple one-off ops |
| Need query optimization | Interactive notebooks |
| Streaming required | Immediate feedback |
Lazy benefits: predicate pushdown, projection pushdown, parallel execution, streaming.
Style: Use .alias() for Column Naming
Always use .alias("name") instead of name=expr kwargs:
df.with_columns(
(pl.col("price") * pl.col("volume")).alias("value"),
pl.col("price").mean().over("symbol").alias("avg_price")
)
df.with_columns(
value=pl.col("price") * pl.col("volume"),
avg_price=pl.col("price").mean().over("symbol")
)
.alias() is explicit, chains with other methods, and works consistently in all contexts.
Anti-Patterns - AVOID
df.with_columns(pl.col("x").map_elements(lambda x: x * 2))
df.with_columns((pl.col("x") * 2).alias("x"))
for row in df.iter_rows():
process(row)
df.with_columns(process_expr)
lf.filter(...).collect().select("a", "b")
lf.select("a", "b").filter(...).collect()
Performance Checklist
Reference Navigator
Python References
| Topic | File | When to Load |
|---|
| Expressions, types, lazy/eager | python/core_concepts.md | Understanding fundamentals |
| Select, filter, group_by, window | python/operations.md | Common operations |
| CSV, Parquet, streaming I/O | python/io_guide.md | Reading/writing data |
| Joins, pivots, reshaping | python/transformations.md | Combining/reshaping data |
| Performance, patterns | python/best_practices.md | Optimization |
Rust References
Shared References
Migration Guides
Time Series / Financial Data Quick Patterns
df.group_by_dynamic("timestamp", every="1m").agg(
pl.col("price").first().alias("open"),
pl.col("price").max().alias("high"),
pl.col("price").min().alias("low"),
pl.col("price").last().alias("close"),
pl.col("volume").sum()
)
df.with_columns(
pl.col("price").rolling_mean(window_size=20).alias("sma_20"),
pl.col("price").rolling_std(window_size=20).alias("volatility")
)
trades.join_asof(quotes, on="timestamp", by="symbol", strategy="backward")
Load python/best_practices.md for comprehensive time series patterns.
Runnable Examples
Development Tips
Use LSP for navigating Polars code:
- Python: Pyright/Pylance provides excellent type inference for Polars expressions
- Rust: rust-analyzer understands Polars types and expression chains
LSP operations like goToDefinition and hover help explore Polars API without leaving the editor.