Home › Python Data & ML › Polars DataFrame rules

Polars DataFrame rules

Cursor rules for Polars (1.x/2.x) data pipelines in Python: lazy queries, expressions, schemas, and no row-wise Python.

CursorPython Data & ML

How to use

Save this as .cursor/rules/polars.mdc in your repository root, commit it, and your agent will pick it up automatically. Edit the commands to match your project.

Note: Review and test before use. Provided as is, without warranty. See Terms.

.cursor/rules/polars.mdcDownload  
---
description: Polars DataFrame conventions
globs: ["**/*.py","notebooks/**/*.ipynb"]
alwaysApply: false
---

# Polars

- Check the installed Polars version (`pl.__version__`) and follow its API; deprecated names change between majors.
- Prefer the lazy API: start with `pl.scan_parquet`/`scan_csv`/`scan_ndjson`, build the query, call `.collect()` once at the end. Use `.collect(engine="streaming")` for larger-than-memory data if the version supports it.
- Use expressions, not Python loops: `df.with_columns((pl.col("price") * pl.col("qty")).alias("total"))`, `pl.when(...).then(...).otherwise(...)`, `group_by(...).agg(...)`.
- Avoid `map_elements`/`apply` (row-wise Python). If unavoidable, say why and set `return_dtype`.
- Declare schemas/dtypes when reading (`schema_overrides=`) and cast early; use `pl.Categorical`/`pl.Enum` for low-cardinality strings and proper `pl.Datetime(time_zone="UTC")`.
- Filter and select columns as early as possible (projection/predicate pushdown).
- Joins: specify `how` and `validate="m:1"` etc. to catch duplicate keys; use `join_asof` for time-series alignment.
- Don't convert to pandas unless a downstream library requires it (`.to_pandas()` at the very end).
- Inspect plans with `lf.explain()` when performance matters.
- Write outputs as Parquet with compression; partition large outputs by a column.

## Done when
Tests on small fixture frames pass (`polars.testing.assert_frame_equal`), and the pipeline runs end to end on sample data.

More: all Python Data & ML rules · all Cursor rules

Related rules

Jupyter notebook hygiene rules

Cursor rules for Jupyter notebooks in data projects: reproducible cells, small outputs, and moving code into modules.

CursorPython Data & ML

pandas rules

Cursor rules for pandas 2.2+/3.x analysis code: Copy-on-Write, vectorized ops, dtypes, method chaining, and validation.

CursorPython Data & ML

Python data & ML CLAUDE.md

CLAUDE.md for data science / ML repos using pandas or polars, notebooks, and reproducible pipelines.

Claude Code (CLAUDE.md)Python Data & ML

PyTorch training CLAUDE.md

CLAUDE.md for PyTorch 2.x model training repos: reproducible configs, data loaders, mixed precision, and checkpoints.

Claude Code (CLAUDE.md)Python Data & ML

Streamlit app AGENTS.md

AGENTS.md for Streamlit data apps: the rerun model, caching, session state, forms and fragments, secrets, and testing.

AGENTS.mdPython Data & ML

LangGraph agent rules

Cursor rules for building agents with LangGraph in Python: typed state, nodes, tools, checkpointers, and tests.

CursorLLM apps & agents

Get new rules weekly

One email a week: fresh rule files, agent tips, and what's changing in Cursor, Claude Code and Copilot. Free, unsubscribe anytime.

Opens the Rulestack Newsletter on beehiiv in a new tab. Or subscribe directly on beehiiv.