What is ModernDiD?#
Difference-in-differences (DiD) is one of the most widely used methods for causal inference from observational data. The modern DiD literature has produced many estimators, but implementations are scattered across separate R and Stata packages with incompatible APIs and output formats.
ModernDiD brings them together into a single Python library with a consistent API for applied researchers, economists, and data scientists. If you already use R packages for DiD, ModernDiD gives you the same estimators in Python with a unified interface. If you are new to DiD, the introduction covers the key ideas.
Every estimator follows the same three-step workflow of estimate, aggregate, visualize. Switching between designs means changing one function call, not learning a new package.
import moderndid as did
# Works with pandas, polars, pyarrow, duckdb, or any Arrow-compatible DataFrame
data = did.load_mpdta()
# Estimate group-time ATTs
result = did.att_gt(
data=data,
yname="lemp",
tname="year",
idname="countyreal",
gname="first.treat",
)
# Aggregate into an event study
event_study = did.aggte(result, type="dynamic")
# Visualize
did.plot_event_study(event_study)
Switching between estimators means changing one function call. The aggregation and plotting interface stays the same.
Key features#
Consistent API. Every estimator returns a typed result object with the same interface for summarizing, aggregating, and plotting. Learn the pattern once and apply it everywhere.
DataFrame agnostic. Pass any Arrow-compatible DataFrame such as polars, pandas, pyarrow, duckdb, and more, powered by narwhals.
Scales up. Runs locally on a laptop, then transparently scales to multi-node Dask and Spark clusters for datasets that exceed single-machine memory. Just pass a Dask or Spark DataFrame and the distributed backend activates automatically.
Fast computation. Polars for internal data wrangling, NumPy vectorization, Numba JIT compilation, and threaded parallel compute.
GPU acceleration. Optional CuPy-accelerated regression and propensity score estimation on NVIDIA GPUs, with multi-GPU scaling in distributed environments. See the GPU guide.
Native plots. Built-in plotnine visualizations returning standard
ggplotobjects you can customize with the full grammar of graphics.
Estimators#
ModernDiD covers the main DiD designs used in applied work. Each estimator targets a different treatment structure. See the estimator overview for detailed descriptions, key arguments, and guidance on when to use each one.
Design |
Reference |
|---|---|
Staggered adoption ( |
|
Two-period DR DiD ( |
|
Continuous treatment ( |
|
Triple differences ( |
|
Intertemporal treatment ( |
|
Dynamic covariate balancing DiD ( |
|
Sensitivity analysis ( |
|
Extended TWFE ( |
|
Nonparametric IV ( |
Next steps#
Installation for detailed install options and optional extras.
Quickstart to learn the API with working examples.
Introduction to DiD for background on the difference-in-differences framework.