diff-diff: Difference-in-Differences in Python#
diff-diff is a Python library for Difference-in-Differences (DiD) causal inference analysis. It provides sklearn-like estimators with statsmodels-style output for econometric analysis.
from diff_diff import DifferenceInDifferences
# Fit a basic DiD model
did = DifferenceInDifferences()
results = did.fit(data, outcome='y', treatment='treated', time='post')
print(results.summary())
Key Features#
13+ Estimators: Basic DiD, TWFE, Event Study, Synthetic DiD, plus modern staggered estimators (Callaway-Sant’Anna, Sun-Abraham, Imputation, Two-Stage, Stacked DiD), advanced methods (TROP, Continuous DiD, Efficient DiD, Triple Difference), and Bacon Decomposition diagnostics
Modern Inference: Robust standard errors, cluster-robust SEs, wild cluster bootstrap, and multiplier bootstrap
Assumption Testing: Parallel trends tests, placebo tests, Bacon decomposition, and comprehensive diagnostics
Sensitivity Analysis: Honest DiD (Rambachan & Roth 2023) for robust inference under parallel trends violations
Built-in Datasets: Real-world datasets from published studies (Card & Krueger, Castle Doctrine, and more)
High Performance: Optional Rust backend for compute-intensive estimators like Synthetic DiD and TROP
Publication-Ready Output: Summary tables, event study plots, and sensitivity analysis figures
Installation#
pip install diff-diff
For development:
pip install diff-diff[dev]
Quick Links#
Getting Started: Measuring Campaign Impact - Measuring campaign impact? Start here
Which Analysis Method Fits Your Problem? - Which method fits your business problem?
Getting Started - Installation and your first DiD analysis
Choosing an Estimator - Which estimator should I use?
aggregate_survey()- Have BRFSS/ACS/CPS microdata? Bridge it to a geographic panel for DiDBasic Difference-in-Differences with diff-diff - Hands-on basic tutorial
Troubleshooting - Common issues and solutions
R Comparison - Coming from R?
API Reference - Full API reference
What is Difference-in-Differences?#
Difference-in-Differences (DiD) is a quasi-experimental research design that estimates causal treatment effects by comparing outcome changes over time between treated and control groups. It is one of the most widely used methods in applied economics, public policy evaluation, and social science research.
Why diff-diff?#
Complete method coverage: 13+ estimators from basic 2x2 DiD to cutting-edge methods like Efficient DiD (Chen et al. 2025) and TROP (Athey et al. 2025)
Familiar API: sklearn-like
fit()interface — if you know scikit-learn, you know diff-diffModern staggered methods: Callaway-Sant’Anna, Sun-Abraham, Imputation DiD, Two-Stage DiD, and Stacked DiD handle heterogeneous treatment timing correctly
Robust inference: Heteroskedasticity-robust, cluster-robust, wild cluster bootstrap, and multiplier bootstrap
Sensitivity analysis: Honest DiD (Rambachan & Roth 2023) for robust inference under parallel trends violations
Validated against R: Benchmarked against
did,synthdid, andfixest— see BenchmarksNo heavy dependencies: Only numpy, pandas, and scipy
Supported Estimators#
Estimator |
Description |
|---|---|
Basic 2x2 DiD with robust/clustered standard errors |
|
Panel data with unit and time fixed effects |
|
Event study with period-specific treatment effects |
|
Callaway & Sant’Anna (2021) for staggered adoption |
|
Sun & Abraham (2021) interaction-weighted estimator |
|
Borusyak, Jaravel & Spiess (2024) imputation estimator |
|
Gardner (2022) two-stage residualized estimator |
|
Butts (2021) ring-indicator spillover-aware DiD |
|
Synthetic DiD combining DiD and synthetic control |
|
Wing, Freedman & Hollingsworth (2024) stacked DiD |
|
Chen, Sant’Anna & Xie (2025) efficient DiD |
|
Triple difference (DDD) estimator |
|
Continuous treatment DiD |
|
Triply Robust Panel with factor model adjustment (Athey et al. 2025) |
|
Goodman-Bacon decomposition diagnostics |