Skip to main content

diff-stream

Difference-in-differences with staggered treatment timing, on panels larger than memory. Includes Callaway and Sant'Anna, Sun and Abraham, Borusyak, Jaravel and Spiess imputation, Gardner's two-stage DiD, Wooldridge's extended two-way fixed effects, and stacked DiD. Two diagnostics show what a conventional two-way fixed effects estimate averages. Results are tested against the authors' R and Stata packages.

It reads Parquet in a stream, sorts the panel once (in memory when it fits, in whole-unit pieces on disk when it does not), and computes standard errors clustered by unit without holding all units' scores.

from diff_stream import did_callaway_santanna

fit = did_callaway_santanna(
    "earnings ~ age + C(region)",
    "panel/*.parquet",
    panel=("person", "year"),
    cohort="first_treated_year",
    control_group="not_yet_treated",
)
fit.aggregate().summary()           # event study
fit.aggregate("overall").summary()  # overall ATT

Version 0.1.0. The estimators, inference and documentation are complete and tested; the benchmarks compare them with other implementations.

What it does

Estimators (each cites its paper in its docstring)

  • did_callaway_santanna: group-time effects, with outcome regression, IPW or doubly robust covariate adjustment; never-treated or not-yet-treated controls; varying or universal base periods; panels or repeated cross sections.
  • did_sun_abraham: the interaction-weighted event study, with covariates and additional fixed effects.
  • did_bjs: imputation, with the authors' standard errors (residual averaging groups, leave-out) and their pre-trend test.
  • did_two_stage: Gardner's two-stage DiD, event study or overall ATT.
  • did_extended_twfe: Wooldridge's extended TWFE (linear), with covariate-interacted effects.
  • did_stacked: stacked DiD with corrective weights, without building the stack.

Data

  • Unbalanced panels, sampling weights (also time-varying), anticipation, and panels where every unit is eventually treated (the last-treated cohort as comparison).

Results and inference

  • Aggregation by event time, cohort and calendar period, or overall, with balanced event windows and the cells and cohorts behind every estimate.
  • Standard errors clustered by unit, including the uncertainty in the cohort shares used to aggregate; pre-trend tests.
  • Callaway-Sant'Anna's multiplier bootstrap for simultaneous bands, and a bootstrap that refits any estimator on unit resamples, in parallel.

Diagnostics

  • bacon_decomp: the Goodman-Bacon decomposition (with covariates and weights).
  • twfe_weights: de Chaisemartin and D'Haultfoeuille's weights and sensitivity measures.

Resources

  • n_threads= and max_memory_gb= respect a job's limits (stream-limits); profile=True reports the memory and disk a fit needs, without fitting.
  • Results with hdfe-stream's reporting: summary(), tidy(), confint(), wald_test(), etable.

Why it is fast

  • Sorted once. The panel is sorted by unit in one piece when it fits in memory, in whole-unit pieces on disk otherwise; later passes read whole units in large batches.
  • Sufficient statistics. Without covariates, the estimators reduce to cohort-by-period means plus unit scores computed in parallel.
  • Covariance by cohort. A unit's scores touch only its cohort's cells, so the joint covariance of hundreds of effects is accumulated cohort by cohort.
  • Shared passes. All comparisons' propensity and outcome models are fitted together.

On a simulated panel of one million units over eight periods (8 million rows), each estimator fits in one to two seconds on a 16-vCPU cloud instance, and ten million units (80 million rows) in under half a minute, with a peak of about 4 GB. With max_memory_gb=2, a million units fit within about 0.8 GB. The benchmarks give the comparisons with R and Python implementations.

Validation against other implementations

Implementation Compared on
R did, DRDID Callaway-Sant'Anna: effects, full covariance, aggregations, multiplier bootstrap; the mpdta example
R fixest (sunab) Sun-Abraham: coefficients, covariance, aggregation; the Castle Doctrine panel
Stata did_imputation (authors), R didimputation BJS: estimates and full covariance, every averaging group, leave-out, pre-trend leads
R did2s (authors) two-stage: coefficients and covariance
R etwfe, emfx (authors) extended TWFE: effects and the covariance of every aggregation
fixest on the stacked data stacked DiD: coefficients and covariance
R bacondecomp Goodman-Bacon: every comparison; the math_reform example
R TwoWayFEWeights (authors) TWFE weights (feTR) and sensitivity measures

Agreement is within 2e-7 absolute, usually 1e-10. The frozen references and the R and Stata scripts that make them are in tests/data/ and benchmarks/external/; docs/implementations.md lists the conventions on which these packages differ.

Install

pip install diff-stream

Python 3.10 or later. It depends on hdfe-stream (with its formula extra), stream-limits, Polars, NumPy 2, numba, SciPy and pyarrow.

Documentation

Build the documentation locally with:

pip install -e ".[docs]"
sphinx-build -W --keep-going -b html docs docs/_build/html

It covers choosing an estimator, your data, each estimator, results and inference, the diagnostics, worked examples with their output, how it works, memory and threads, the conventions that differ from other packages, and the validation. examples/ has runnable scripts, and benchmarks/ the performance comparisons (see benchmarks/README.md).

A sibling of hdfe-stream, logit-stream, dpd-stream and match-stream.

License

MIT.

Metadata

Release files for diff-stream 0.1.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for diff-stream 0.1.0
File Size Uploaded Provenance Actions
diff_stream-0.1.0.tar.gz 221.6 kB With provenance Details for diff_stream-0.1.0.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for diff-stream 0.1.0
File Interpreter ABI Platform Actions
diff_stream-0.1.0-py3-none-any.whl Python 3 none any Details for diff_stream-0.1.0-py3-none-any.whl

Total release size: 393.4 kB

Release files / diff_stream-0.1.0.tar.gz

Download URL diff_stream-0.1.0.tar.gz
Size 221.6 kB
Tags Source
SHA-256 checksum
How to use checksums
d4abff27d5ce3f15a291f4d1247ac808e3b43fb465320ae227e452c5e2a563bf
BLAKE2b-256 checksum
How to use checksums
87c085ee958a95be5ad48459d485ee2db747e387f4087b246eb6b1a84ba6f95c
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Nov 10, 2026.

Transparency log

Release files / diff_stream-0.1.0-py3-none-any.whl

Download URL diff_stream-0.1.0-py3-none-any.whl
Size 171.9 kB
Tags Python 3
SHA-256 checksum
How to use checksums
dbc924456199690fbcd4849136a86e01af48140d3f26a23f688123a8c83db09d
BLAKE2b-256 checksum
How to use checksums
6f10fe45e9a66a6fa1fdf93ae8f77ab10261219a0d2b9c133ed5e97bc2c531c2
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Nov 10, 2026.

Transparency log

Security for diff-stream 0.1.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

Full provenance

Both files in this release have been uploaded with provenance metadata.

Select from 2 files

Attestation for diff_stream-0.1.0.tar.gz

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Nov 10, 2026.

Transparency log

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page