moderndid.core.panel.get_first_difference#

moderndid.core.panel.get_first_difference(data: Any, idname: str, yname: str, tname: str) Any[source]#

Add a "dy" column containing first-differenced outcomes.

First-differencing computes \(\Delta Y_{it} = Y_{it} - Y_{i,t-1}\) for each unit, removing time-invariant unit fixed effects. The att_gt estimator performs this step internally, but exposing it here allows inspection of the transformed data before estimation.

Parameters:
dataDataFrame

Panel data. Accepts any object implementing the Arrow PyCapsule Interface (__arrow_c_stream__), including polars, pandas, pyarrow Table, and cudf DataFrames.

idnamestr

Unit identifier column.

ynamestr

Outcome column.

tnamestr

Time period column.

Returns:
DataFrame

Original columns plus "dy", in the same format as data.

See also

att_gt

Estimate group-time average treatment effects.

Examples

In [1]: from moderndid import get_first_difference, load_favara_imbs
   ...: 
   ...: df = load_favara_imbs()
   ...: df = get_first_difference(df, idname="county", yname="Dl_vloans_b", tname="year")
   ...: df.select("county", "year", "Dl_vloans_b", "dy").head(10)
   ...: 
Out[1]: 
shape: (10, 4)
┌────────┬──────┬─────────────┬───────────┐
│ county ┆ year ┆ Dl_vloans_b ┆ dy        │
│ ---    ┆ ---  ┆ ---         ┆ ---       │
│ i64    ┆ i64  ┆ f64         ┆ f64       │
╞════════╪══════╪═════════════╪═══════════╡
│ 1001   ┆ 1994 ┆ 0.270248    ┆ null      │
│ 1001   ┆ 1995 ┆ -0.038427   ┆ -0.308675 │
│ 1001   ┆ 1996 ┆ 0.161633    ┆ 0.20006   │
│ 1001   ┆ 1997 ┆ 0.056523    ┆ -0.10511  │
│ 1001   ┆ 1998 ┆ 0.034236    ┆ -0.022287 │
│ 1001   ┆ 1999 ┆ 0.048719    ┆ 0.014483  │
│ 1001   ┆ 2000 ┆ -0.034618   ┆ -0.083337 │
│ 1001   ┆ 2001 ┆ -0.010966   ┆ 0.023652  │
│ 1001   ┆ 2002 ┆ 0.121278    ┆ 0.132244  │
│ 1001   ┆ 2003 ┆ 0.202691    ┆ 0.081413  │
└────────┴──────┴─────────────┴───────────┘