Polars – Fast DataFrame Library Cheatsheet
Polars ist eine High-Performance DataFrame-Bibliothek in Rust geschrieben mit Python-, R- und Node-Bindings. Es ist standardmäßig Multi-Threaded, auf Apache Arrow spalten-basiertem Speicher aufgebaut und bietet eine Lazy API mit einem Query-Optimizer, der die Arbeit vor der Ausführung neu ordnet und beschneidet. Bei großen Datensätzen ist es typischerweise viele Male schneller als Pandas und verwendet weniger Speicher, während seine Expression-API sauberer zusammengesetzt wird. Es ist 2026 die Standard-Empfehlung für neue Datenarbeit geworden.
Installation
| Methode | Befehl |
|---|
| pip | pip install polars |
| uv | uv add polars |
| Mit Extras | pip install "polars[all]" (excel, database, plotting) |
| Überprüfen | python -c "import polars as pl; print(pl.__version__)" |
Lesen & Schreiben
| Operation | Code |
|---|
| CSV | pl.read_csv("data.csv") |
| Parquet | pl.read_parquet("data.parquet") |
| JSON | pl.read_json("data.json") |
| Database | pl.read_database(query, connection) |
| Lazy CSV | pl.scan_csv("data.csv") (aufgeschoben) |
| Lazy Parquet | pl.scan_parquet("*.parquet") |
| Schreiben | df.write_parquet("out.parquet") |
Eager vs Lazy (die Kernidee)
import polars as pl
# Eager: führt sofort aus
df = pl.read_csv("sales.csv")
result = df.filter(pl.col("amount") > 100).group_by("region").agg(pl.col("amount").sum())
# Lazy: baut einen Plan, optimiert, dann läuft auf .collect()
result = (
pl.scan_csv("sales.csv")
.filter(pl.col("amount") > 100)
.group_by("region")
.agg(pl.col("amount").sum())
.collect()
)
Lazy-Modus lässt den Optimizer Filter nach unten zum Scan drücken und liest nur benötigte Spalten – oft eine große Speedup bei großen Dateien. Bevorzugen Sie scan_* + .collect() für echte Workloads.
Expressions (die Kern-API)
| Expression | Macht |
|---|
pl.col("x") | Referenzieren Sie eine Spalte |
pl.col("x").sum() | Aggregieren |
pl.col("x").alias("y") | Ausgabe umbenennen |
pl.col("*") | Alle Spalten |
pl.lit(5) | Literal Wert |
pl.when(cond).then(a).otherwise(b) | Konditional |
Auswählen & Filtern
| Aufgabe | Code |
|---|
| Spalten wählen | df.select("a", "b") |
| Berechnete Spalte | df.with_columns((pl.col("a") * 2).alias("a2")) |
| Reihen filtern | df.filter(pl.col("a") > 10) |
| Mehrere Bedingungen | df.filter((pl.col("a") > 10) & (pl.col("b") == "x")) |
| Head/Tail | df.head(10), df.tail(10) |
| Sortieren | df.sort("a", descending=True) |
| Eindeutig | df.unique(subset=["a"]) |
Aggregation & Gruppierung
(df.group_by("region")
.agg(
pl.col("amount").sum().alias("total"),
pl.col("amount").mean().alias("avg"),
pl.col("order_id").n_unique().alias("orders"),
))
| Aggregation | Expression |
|---|
| Sum/Mean/Min/Max | .sum(), .mean(), .min(), .max() |
| Count | .count(), .n_unique() |
| First/Last | .first(), .last() |
| Quantile | .quantile(0.95) |
| List Collect | .implode() |
Joins
| Typ | Code |
|---|
| Inner | a.join(b, on="id") |
| Left | a.join(b, on="id", how="left") |
| Outer | a.join(b, on="id", how="full") |
| Anti | a.join(b, on="id", how="anti") |
| Semi | a.join(b, on="id", how="semi") |
| As-of (Zeit) | a.join_asof(b, on="ts") |
Strings, Dates, Lists
| Namespace | Beispiel |
|---|
.str | pl.col("s").str.to_uppercase(), .str.contains("x") |
.dt | pl.col("t").dt.year(), .dt.truncate("1h") |
.list | pl.col("l").list.len(), .list.first() |
.cast | pl.col("a").cast(pl.Int64) |
Interop
| Ziel | Code |
|---|
| Pandas | df.to_pandas() / pl.from_pandas(pdf) |
| Arrow | df.to_arrow() |
| NumPy | df.to_numpy() |
| DuckDB | Fragen Sie Polars Frames direkt von DuckDB ab |
Polars vs Pandas
| Aspekt | Polars | Pandas |
|---|
| Engine | Rust, Multi-Threaded | Python/C, meist Single-Threaded |
| Memory | Arrow Spalten | NumPy-unterstützt |
| Lazy Optimizer | Ja | Nein |
| API | Expression-basiert, zusammensetzbar | Index-zentrisch |
| Ökosystem | Wächst schnell | Riesig, reif |
Für SQL-First Analytics über Dateien paaren Sie mit DuckDB; Pandas bleibt wertvoll für seine Ökosystem-Breite.
Ressourcen