| Crates.io | polars-formula |
| lib.rs | polars-formula |
| version | 0.3.9 |
| created_at | 2025-08-21 18:54:43.857881+00 |
| updated_at | 2025-08-30 20:33:05.133529+00 |
| description | High-performance formula parsing and materialization library for Polars DataFrames with R/Formulaic/Patsy style syntax |
| homepage | https://github.com/alexhallam/polars-formula |
| repository | https://github.com/alexhallam/polars-formula |
| max_upload_size | |
| id | 1805227 |
| size | 922,692 |
formula in model-matrix out
A formula parsing and materialization library for Rust that brings R-style/Formulaic/Patsy formula syntax to the Polars DataFrame ecosystem.
This library is in development and is not yet ready for production use.
I wanted to work with formulas in Rust, using formula syntax. This library aims to fill that gap by providing a way to parse and materialize formulas directly into Polars DataFrames.
y ~ x1 + x2 + x1:x2 + poly(x1, 3) - 1Only four functions are exposed:
canonicalize() - Convert a formula string into its canonical form
materialize() - Convert a formula and DataFrame into response vector and design matrix
print_formula() - Print a formula with syntax highlighting
print_modelspec() - Print a model specification
Run cargo add polars-formula or add this to your Cargo.toml:
[dependencies]
polars-formula = "0.3.8"
polars = { version = "0.50", features = ["lazy"] }
// Example 01
// ==========
// If you git clone this repo, you can run this example with:
// git clone https://github.com/alexhallam/polars-formula.git
// cd polars-formula
// cargo run --example 01
use polars::prelude::*;
use polars_formula::{canonicalize, materialize, print_formula, print_modelspec};
fn main() -> Result<(), Box<dyn std::error::Error>> {
println!("=== Simple API Demo ===\n");
// Load data
let df = CsvReader::new(std::fs::File::open("examples/data/mtcars.csv")?).finish()?;
// Formula string
let formula_str = "mpg ~ cyl + wt*hp + poly(disp, 4) - 1";
// Step 1: Canonicalize (parse and canonicalize)
println!("\n1. Parse and canonicalize formula");
let spec = canonicalize(formula_str)?;
print_formula(&spec);
// Step 2: Print the full model spec
println!("\n2. Full model specification:");
print_modelspec(&spec);
// Step 3: Materialize the formula
println!("\n3. Materializing formula");
let (y, x, _z) = materialize(&spec, &df)?;
println!(" Results: y={}\n X={}\n", y, x);
Ok(())
}
Run the examples to see the library in action:
cargo run --example 01
cargo run --example 02
cargo run --example 03
x, income, agex1 + x2 (include both terms)x1:x2 (product of x1 and x2)x1 * x2 (expands to x1 + x2 + x1:x2)-1 to remove)poly(x, 3) expands to x, xยฒ, xยณI(x) for literal interpretation1, 0 for intercept control(1|group) - one random effect per group level(x|group) - random slope for variable x per group(x||group) - uncorrelated random effectsy ~ x, family=gaussian()y ~ x + sigma ~ zy ~ x + ar(p=1)C(category) for factor encodings(x, k=10) for smooth function approximationsContributions are welcome! Please feel free to submit a Pull Request. For major changes, please open an issue first to discuss what you would like to change.
This project is licensed under the MIT License - see the LICENSE file for details.