README.md

xpd

Run uvx xpd check . to find the lines of your Python code that are slow or give a wrong result, and add --fix to let xpd rewrite the ones it can. xpd checks code that uses pandas, NumPy, SciPy, scikit-learn, scikit-lego, MAPIE, Category Encoders, linearmodels, arch, pymoo, plotly or Apache Hamilton. It reads the files and starts no Python, so you or a coding agent can run it after every edit.

The documentation is at https://bjoernaagaard.github.io/xpd/, with a page for every rule.

Install

Install the xpd command with uv.

sh
1uv tool install xpd

You can also run xpd once without installing it, with uvx xpd in place of xpd.

Check a file and fix it

Save the following code as bad.py in an empty folder. xpd reads the code and doesn't run it, so you don't need pandas.

python
1import pandas as pd
2
3df = pd.DataFrame({"a": [1, 2], "k": [1, 2]})
4other = df.copy()
5for i, row in df.iterrows():
6 df["a"][i] = row["a"] * 2
7df.dropna(inplace=True)
8m = pd.merge(df, other, on="k")
9out = pd.DataFrame()
10for f in ["x.csv"]:
11 out = pd.concat([out, pd.read_csv(f)])

Run xpd on the file. It prints one line for each issue, with the place, the severity, the rule and what to write instead, and it exits with 1 because one issue is an error.

console
1$ xpd check bad.py
2bad.py:5:15: warning [pandas/row-loop] iterrows() builds a Series for every row. Compute on whole columns instead, such as df["a"] * df["w"]. When each row has to become a Python object, loop over itertuples() or to_dict("records").
3bad.py:6:5: error [pandas/chained-assignment] Chained assignment writes to a copy under copy-on-write, so the frame keeps its old values. Assign to df.loc[i, "a"] instead.
4bad.py:7:1: info [pandas/no-inplace] inplace=True gives no speedup and blocks chaining. Reassign instead.
5bad.py:8:5: warning [pandas/merge-without-validate] merge without validate= can silently duplicate rows.
6bad.py:11:5: warning [pandas/concat-in-loop] out grows by one concat per pass, and each concat copies every earlier row. Collect the frames in a list and concat once after the loop.
7xpd: 5 issues, 2 more with --fix --unsafe-fixes

Fix every error, and fix a warning unless you can say why the code is right in that place.

SeverityMeaning
errorThe code gives a wrong result or raises an exception
warningThe code is slow or easy to get wrong
infoA shorter or newer form exists

xpd check bad.py --fix writes the safe fixes, which keep what the code does. None of the five issues has one, so the file stays as it was. --unsafe-fixes adds the fixes that may change what the code does, so read the diff after you use it.

console
1$ xpd check bad.py --fix --unsafe-fixes
2bad.py:5:15: warning [pandas/row-loop] iterrows() builds a Series for every row. Compute on whole columns instead, such as df["a"] * df["w"]. When each row has to become a Python object, loop over itertuples() or to_dict("records").
3bad.py:8:5: warning [pandas/merge-without-validate] merge without validate= can silently duplicate rows.
4bad.py:11:5: warning [pandas/concat-in-loop] out grows by one concat per pass, and each concat copies every earlier row. Collect the frames in a list and concat once after the loop.
5xpd: fixed 2, 3 issues

xpd rewrote lines 6 and 7 and exited with 0, because no error is left.

diff
1 for i, row in df.iterrows():
2- df["a"][i] = row["a"] * 2
3-df.dropna(inplace=True)
4+ df.loc[i, "a"] = row["a"] * 2
5+df = df.dropna()

Under pandas copy-on-write, the old line 6 left df unchanged, and the new line writes the doubled values into df. xpd check exits with 0 when it finds no error, with 1 when it finds one, and with 2 when it can't run. The Get started page explains each part of an issue.

Rules

Each library has a pack of rules, and a pack runs only on a file that imports its library. The Python pack runs on every file. Each rule has a page that says what goes wrong, what to write instead and whether xpd can fix it, such as pandas/chained-assignment.

PackOne of its rulesWhat the rule reports
pandaschained-assignmentdf["a"][i] = v writes to a copy, so the frame keeps its old values
NumPyappend-flattensnp.append without axis flattens both arrays
SciPyremoved-keywordtol= on the sparse iterative solvers was removed and raises TypeError
scikit-learnfit-on-testA fit on the test rows scored 0.997 AUC on data with no signal
MAPIEcalibrate-on-training-rowsconformalize on the training rows covered 0.468 of the test rows where 0.9 was asked
plotlymarker-size-diameterA marker's size is its diameter, so markers of size 10 and 20 had areas of 78.5 and 314.2 square pixels
Pythondict-str-anydict[str, Any] in a signature hides the keys the function uses

xpd rules lists every rule, and the rules overview lists the packs with their counts.

Some info rules state a preference of the house style, such as one method chain where the code builds a frame in separate steps. Set style to make xpd report them as warnings, in [tool.xpd] of the project's pyproject.toml or at the top of ~/.config/xpd/config.toml. xpd check describes the setting.

toml
1[tool.xpd]
2style = "strict"

Commands

xpd <command> -h lists the options of a command, and the command reference has a page for each one.

text
1xpd
2├── lint
3│ ├── check [paths] lint files and folders, and rewrite with --fix
4│ ├── rules list every rule with its documentation
5│ └── hook answer an agent's session: lint each edit, and hold a turn that leaves errors or warnings
6├── look up a library
7│ ├── find "<task>" find library symbols, rules and worked examples for a task
8│ ├── api <name> read one symbol, method or worked example in full
9│ └── index <source> build a search index from library source
10├── read and rewrite code
11│ ├── run [paths] find a code pattern, and preview or apply a rewrite
12│ ├── outline [paths] show imports, definitions and members with line numbers
13│ └── graph <files> print the dataflow of step code or Hamilton modules
14├── run the project's Python
15│ ├── profile <data file> print dtypes, nulls, unique counts and memory per column
16│ ├── equiv old.py new.py run two scripts and compare the `result` each one leaves
17│ └── refit <contract> refit a saved Pipeline on raw training rows and compare
18├── write your own rules
19│ ├── new create a rule project, a rule, a test case or a utility
20│ └── test check rule examples and compare snapshots
21└── connect a tool
22 ├── mcp start the MCP server for agents
23 ├── lsp start the language server for editors
24 ├── skill print the agent skill for the installed version
25 ├── doctor suggest Ruff, ty and pytest settings for the project
26 └── completions <shell> print shell completion

profile, equiv and refit run the project's Python, so they need pandas in the project's environment. index runs Python with Griffe, and the first find downloads a 333 MB search model once. Every other command only reads files.

Use xpd with an agent or an editor

You wantDoGuide
An agent to see the issues of each file it edits, and to keep working while one has an error or a warningInstall the plugin, or add xpd hook to the agent's hooksClaude Code and other agents
An agent to call xpd's toolsclaude mcp add xpd -- uvx xpd mcpConnect an agent with MCP
The hook, the MCP server and the skills in one stepInstall this repository as a plugin in Claude Code or CodexAgent plugin
An agent to know when to run each commandnpx skills add bjoernaagaard/xpdAgent skill
An agent to train a scikit-learn model with checksxpd mcp --code-modeTrain a model in code mode
Issues, fixes and rule help in your editorStart xpd lsp from the editorUse xpd in an editor
A rule for your own projectxpd new ruleWrite a rule

Install the plugin in Claude Code with the following commands. The guide has the commands for Codex.

sh
1claude plugin marketplace add bjoernaagaard/xpd
2claude plugin install xpd@xpd

The plugin runs xpd hook at four points of a session.

text
1session starts xpd prints which rule packs apply, the style setting,
2 and the pipelines and Hamilton nodes of the project
3user sends a prompt xpd prints the playbook that answers the prompt, such as
4 xpd/recipe/plotly-chart for "improve my charts",
5 or the open issues when the prompt asks what to improve
6agent edits a file xpd reports the issues of that file
7agent wants to stop xpd lints every Python file the session changed,
8 and holds the turn while one has an error or a warning

With style = "strict", a rule of the house style counts as a warning, so the agent also keeps working until the code is in the style.

A playbook is one of xpd's own recipes. It holds the house style for a kind of task as a complete module, with the commands to run in order and the rules that the code satisfies. xpd find "<task>" returns a playbook when the task fits it, and xpd api <name> prints it.

PlaybookWhat its code is
xpd/recipe/pandas-pipelineStep functions that each return frame.assign(...), joined with pipe
xpd/recipe/feature-moduleOne typed function for each feature, named for the column it returns
xpd/recipe/linear-scriptA script that changed a frame line by line, as one chain that xpd equiv proves equal
xpd/recipe/plotly-chartA line chart that reports no issue of the plotly pack

License

You may use xpd's own code under the MIT license or the Apache-2.0 license, and the other parts that ship with xpd keep the licenses in the table.

PartLicenseWhere the text is
xpd's own code and its playbooksMIT or Apache-2.0, as you chooseLICENSE-MIT and LICENSE-APACHE
Recipes, which are examples taken from library documentationThe license of the library, which is MIT, BSD-3-Clause, Apache-2.0 or NCSAlicenses/, one file per library. xpd api <recipe> prints the license name and the source link with the code
option-ext, a dependency shipped unmodifiedMPL-2.0licenses/option-ext-LICENSE.txt, with the complete source in licenses/option-ext-0.2.0-source.tar.gz and at https://crates.io/api/v1/crates/option-ext/0.2.0/download
libcst, the parser that checks a fixed fileMIT, with parts of CPython under PSF-2.0licenses/libcst-LICENSE
EmbeddingGemma, the model that find downloadsThe Gemma Terms of Use at https://ai.google.dev/gemma/termsNot shipped with xpd

The licenses/ folder ships in the crate, the sdist and the wheel, and the package metadata lists the combined licenses.