polars-statistics

Note: This extension is in early stage development. APIs may change and some features are experimental.
High-performance statistical hypothesis testing and regression for Polars DataFrames, powered by Rust — with results validated against R.
Usable from Python (as a Polars plugin and an sklearn-style class API) and from Rust (as an rlib that other Rust crates depend on directly — see Use from Rust).
The repository includes a Claude Code plugin with five skills that document the Python API (expressions, model classes, result fields) for the Claude Code assistant.
Why polars-statistics?
- Polars-native. Everything is a Polars expression, so
group_by, over, and lazy evaluation just work — fit one model per group across millions of groups in a single pipeline, no Python loop.
- sklearn-compatible. Every model class follows the familiar
fit / predict / score contract (regressors → R², classifiers → accuracy). Porting from scikit-learn is mostly a rename — see the sklearn migration guide.
- Ergonomic results. No manual
.struct.field(...) archaeology: call .to_dict(), .summary(), or the ps.unnest(df) helper and get flat, readable output.
- Typed API. Ships
py.typed and full .pyi stubs, so your editor autocompletes every constructor, method, and getter with accurate signatures.
- Validated against R. Where the backing
anofox-* crates supply references, results are checked against R (stats::lm, irr::icc, …) — with real inference (std errors, t-stats, p-values, CIs) that many libraries omit.
Installation
pip install polars-statistics
Quick Start
Fit one model per group as a lazy Polars expression — the thing that sets
polars-statistics apart:
import polars as pl
import polars_statistics as ps
df = pl.DataFrame({
"region": ["north"] * 50 + ["south"] * 50,
"sales": [...],
"spend": [...],
"price": [...],
})
# One OLS per region, computed in parallel across groups.
models = df.group_by("region").agg(
ps.ols("sales", "spend", "price").alias("model")
)
# Flatten the result struct into plain columns in one call.
ps.unnest(models, "model") # region, intercept, coefficients, r_squared, ...
Or use the sklearn-style class API for a train / predict / score loop:
from polars_statistics import OLS
model = OLS().fit(X_train, y_train) # X: 2-D array, y: 1-D array
model.predict(X_test) # -> numpy array
model.score(X_test, y_test) # -> R^2
model.to_dict() # every statistic as a plain dict
print(model.summary()) # readable text summary
Statistical Tests
Statistical tests are powered by anofox-statistics, providing full API parity with R's statistical functions and validated against R implementations.
# Parametric tests
ps.ttest_ind("treatment", "control", alternative="two-sided")
ps.ttest_paired("before", "after")
# Non-parametric tests
ps.mann_whitney_u("x", "y")
ps.kruskal_wallis("group1", "group2", "group3")
# Normality tests
ps.shapiro_wilk("x")
# Forecast comparison
ps.diebold_mariano("errors1", "errors2", horizon=1)
# Correlation tests
ps.pearson("x", "y") # Pearson correlation with CI
ps.spearman("x", "y") # Spearman rank correlation
ps.kendall("x", "y", variant="b") # Kendall's tau
ps.distance_cor("x", "y") # Distance correlation (detects nonlinear)
ps.partial_cor("x", "y", ["z1", "z2"]) # Partial correlation
# Categorical tests
ps.binom_test(successes=7, n=10, p0=0.5) # Exact binomial test (conf_level=0.95)
ps.chisq_test("counts", n_rows=2, n_cols=2) # Chi-square independence
ps.fisher_exact(a=10, b=2, c=3, d=15) # Fisher's exact test (+ ci_lower/ci_upper)
ps.fisher_exact_conditional(a=10, b=2, c=3, d=15) # R fisher.test conditional MLE odds ratio
ps.mcnemar_test(a=45, b=15, c=5, d=35) # McNemar's test
ps.cohen_kappa("counts", n_categories=3) # Inter-rater agreement
ps.cramers_v("counts", n_rows=3, n_cols=3) # Association strength