Patsy

Describes statistical models using R-style symbolic formulas and builds NumPy design matrices

Library
PyPI
v1.0.2
989stars
BSD-2-Clause

Repository Health

Pre-computed score based on development activity, maintenance, community, maturity, and trend momentum.How we score it →
73/100Good
Development Activity76
Maintenance56
Community72
Maturity60
Momentum28

Technical Analysis

AI-assessed by reading the actual repository — architecture, code quality, innovation, and documentation.How we score it →
71/100Good
Architecture78
Code Quality74
Innovation70
Learning Curve62

Patsy is a Python library for describing statistical models with a small, R-inspired formula language (e.g. "y ~ x1 + x2 + x1:x2") and turning those formulas into the NumPy design matrices that fitting routines actually consume. It handles categorical encoding, interaction terms, spline/polynomial basis expansion, and missing-data handling as part of that translation.

Patsy doesn’t fit models itself — it is the formula-parsing and matrix-building layer that sits underneath higher-level libraries, most notably powering the formula API in statsmodels and providing formula support used by other parts of the PyData statistical stack.

What You Get

  • An R-inspired formula mini-language ("y ~ x1 + x2") for declaring model structure symbolically
  • Automatic design-matrix construction with intercept handling, categorical dummy/contrast coding, and interaction terms
  • Built-in spline and polynomial basis functions (bs(), cr(), natural cubic splines) for non-linear terms
  • Stateful transforms that remember training-data statistics (e.g. centering) so the same formula reproduces consistent matrices on new data
  • Missing-data handling policies that can drop or raise on NaNs during matrix construction

Common Use Cases

  • Powering the R-style formula API in statsmodels for regression and GLM model specification
  • Building consistent train/test design matrices from a single formula in custom modeling pipelines
  • Encoding categorical variables and interaction terms without hand-writing dummy-variable logic
  • Adding spline or polynomial basis expansions to linear models for non-linear relationships

Under The Hood

Architecture: Patsy’s pipeline runs from parse_formula.py/infix_parser.py (tokenizing and parsing the formula string into an AST), through desc.py (turning the AST into a symbolic ModelDesc of terms), to build.py/design_info.py (evaluating terms against a data environment and assembling the final NumPy design matrix), with categorical.py and contrasts.py handling categorical encoding and splines.py/mgcv_cubic_splines.py implementing basis-spline terms. Tech Stack: Pure Python with NumPy as its only required dependency, packaged with classic setuptools/setup.py, and tested via tox across Python versions. Code Quality: Core modules embed their own test_* functions/files (e.g. test_build.py, test_highlevel.py, test_state.py) totaling roughly 148 test functions, exercising formula parsing, spline construction, and regression fixtures directly alongside the implementation. API Design: The primary entry points (dmatrix, dmatrices) take a formula string and a data mapping and return ready-to-use design matrices in one call, but the formula mini-language and stateful-transform model (stateful_transform) require learning Patsy-specific syntax beyond plain NumPy/pandas.

Join founders buildingwith open source

Opinionated takes, migration guides, cost-saving tips, and insights from the open source ecosystem.

Subscribe on Substack
Join 750+ subscribers

Search