$ show pydata/patsy
Patsy is a foundational Python library for statistical modeling, specifically designed to parse R-style formula syntax (e.g., 'y ~ x1 + x2') and construct design matrices (numerical arrays) for regression and machine learning algorithms. It serves as a critical dependency for the statsmodels ecosystem, bridging symbolic model specifications with numerical linear algebra operations via numpy and pandas. The architecture relies on a custom formula parser, a stateful transform protocol for handling splines and categorical coding, and a DesignMatrix ndarray subclass that preserves metadata while maintaining performance. While the codebase demonstrates high craftsmanship in parsing and matrix construction, it is no longer under active feature development, with users recommended to migrate to the successor library 'Formulaic'.
The LLM identified 3 files worth highlighting for reuse:
Implements the stateful transform protocol (memorize_chunk, memorize_finish, transform) allowing data transformations like splines and centering to maintain state across training and prediction phases. This pattern enables incremental processing of large datasets without loading full matrices into memory, a non-obvious engineering solution for memory-constrained statistical modeling.
Defines the DesignMatrix class as an ndarray subclass that attaches semantic metadata (column names, factor info) to numerical arrays. This allows statistical libraries to access column semantics without sacrificing numpy performance, demonstrating a clean separation of concerns between numerical computation and model specification.
Contains a custom tokenizer and infix parser implementing the shunting yard algorithm to handle R-style formula operators (~, +, *, :, etc.) with correct precedence. This is a reusable parsing pattern for domain-specific languages that requires complex operator handling beyond standard regex.