Skip to content
Scenario1 min read

Difference-in-Differences in Python

How to use Causalis for difference-in-differences in Python with PanelDataDID, CallawaySantAnnaDID, panel diagnostics, and common failure modes.

Difference-in-Differences in Python

Use Causalis for difference-in-differences in Python when treated and comparison units are observed before and after treatment. Causalis provides a PanelDataDID data contract and CallawaySantAnnaDID estimator for panel treatment designs.

DiD is a design choice, not just a model choice. The key question is whether untreated potential outcomes would have followed parallel trends for treated and comparison units.

Minimal DiD Example

Data Shape

The panel dataframe should include:

  • unit identifiers;
  • time periods;
  • outcome values;
  • treatment timing or cohort information;
  • optional covariates;
  • optional clustering column for inference.

Diagnostics

If an import is not confirmed in the API reference for your installed version, use the DiD API reference and the canonical notebook as the source of truth.

When Not to Use DiD

  • There is no credible untreated comparison group.
  • There are not enough pre-treatment periods to inspect trends.
  • Treated and control units show sharply different pre-treatment dynamics.
  • Treatment timing is ambiguous or affected by anticipation.
  • Only one treated aggregate unit exists and synthetic control has much stronger pre-treatment fit.

Common Mistakes

  • Treating DiD as valid without checking pre-treatment trends.
  • Ignoring anticipation effects.
  • Mixing incompatible treatment cohorts.
  • Reporting only the average effect without cell support diagnostics.