Home › Python Data & ML › pandas rules

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

How to use

Save this as .cursor/rules/pandas.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/pandas.mdcDownload  
---
description: pandas data analysis conventions
globs: ["**/*.py","**/*.ipynb"]
alwaysApply: false
---

# pandas

- Assume Copy-on-Write semantics (default in pandas 3, opt-in via `pd.options.mode.copy_on_write = True` in 2.x). Never rely on chained assignment (`df[df.a > 0]["b"] = 1`); use `df.loc[mask, "b"] = 1`.
- Vectorize: column arithmetic, `.where`, `np.select`, `.str`/`.dt` accessors, `merge`, `groupby().agg()`. No `iterrows` or row-wise `apply` on large frames.
- Readable pipelines via method chaining with `.assign()`, `.query()`, `.pipe()`; one step per line.
- Set dtypes on read (`dtype=`, `parse_dates=`, `dtype_backend="pyarrow"` where supported). Use `category` for low-cardinality strings and nullable types (`Int64`, `string`) to avoid object columns.
- Datetimes are timezone-aware (`utc=True`), converted for display only.
- Joins: specify `how` and `validate="one_to_one"`/`"many_to_one"`; check row counts before/after.
- `groupby(..., observed=True)` for categoricals; named aggregations (`agg(total=("amount", "sum"))`).
- Handle missing data explicitly (`isna`, `fillna` with a reason), not by silently dropping.
- Keep `inplace=True` out of new code.
- For data that doesn't fit in memory, sample, chunk (`chunksize`), or switch to Polars/DuckDB, and say so.
- Save intermediate data as Parquet, not CSV or pickle.

## Done when
Assertions on shapes and key invariants (no duplicate keys, expected columns) pass, and tests use `pd.testing.assert_frame_equal` on small fixtures.

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

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

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.