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]

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-diff

  • Modern 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, and fixest — see Benchmarks

  • No heavy dependencies: Only numpy, pandas, and scipy

Supported Estimators#

Estimator

Description

DifferenceInDifferences

Basic 2x2 DiD with robust/clustered standard errors

TwoWayFixedEffects

Panel data with unit and time fixed effects

MultiPeriodDiD

Event study with period-specific treatment effects

CallawaySantAnna

Callaway & Sant’Anna (2021) for staggered adoption

SunAbraham

Sun & Abraham (2021) interaction-weighted estimator

ImputationDiD

Borusyak, Jaravel & Spiess (2024) imputation estimator

TwoStageDiD

Gardner (2022) two-stage residualized estimator

SpilloverDiD

Butts (2021) ring-indicator spillover-aware DiD

SyntheticDiD

Synthetic DiD combining DiD and synthetic control

StackedDiD

Wing, Freedman & Hollingsworth (2024) stacked DiD

EfficientDiD

Chen, Sant’Anna & Xie (2025) efficient DiD

TripleDifference

Triple difference (DDD) estimator

ContinuousDiD

Continuous treatment DiD

TROP

Triply Robust Panel with factor model adjustment (Athey et al. 2025)

BaconDecomposition

Goodman-Bacon decomposition diagnostics

Indices and tables#