moderndid.core.panel.wide_to_panel#

moderndid.core.panel.wide_to_panel(data: Any, idname: str, stub_names: list[str], separator: str = '_', tname: str = 'time') Any[source]#

Unpivot wide-format data into a long panel.

Gathers time-varying columns back into long format using the stub names and separator to identify which wide columns belong to each variable and period. All other columns (except idname) are treated as time-invariant and repeated for every period.

Parameters:
dataDataFrame

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

idnamestr

Unit identifier column.

stub_nameslist[str]

Variable-name prefixes that identify the time-varying columns. For example, ["y", "x"] will match y_1, y_2, x_1, x_2, etc.

separatorstr

Delimiter between the stub and the period label. Default "_".

tnamestr

Name for the created time column. Default "time".

Returns:
DataFrame

Long-format panel in the same format as data.

See also

panel_to_wide

Inverse operation (long to wide).

Examples

In [1]: from moderndid import make_balanced_panel, panel_to_wide, wide_to_panel, load_favara_imbs
   ...: 
   ...: df = load_favara_imbs()
   ...: df = make_balanced_panel(df, idname="county", tname="year")
   ...: wide = panel_to_wide(df, idname="county", tname="year")
   ...: long = wide_to_panel(wide, idname="county", stub_names=["Dl_vloans_b", "Dl_hpi"], tname="year")
   ...: long.head(10)
   ...: 
Out[1]: 
shape: (10, 18)
┌────────┬──────┬─────────┬──────────┬───┬────────────────┬───────────────┬─────────────┬──────────┐
│ county ┆ year ┆ state_n ┆ w1       ┆ … ┆ inter_bra_2004 ┆ inter_bra_200 ┆ Dl_vloans_b ┆ Dl_hpi   │
│ ---    ┆ ---  ┆ ---     ┆ ---      ┆   ┆ ---            ┆ 5             ┆ ---         ┆ ---      │
│ i64    ┆ i32  ┆ i64     ┆ f64      ┆   ┆ i64            ┆ ---           ┆ f64         ┆ f64      │
│        ┆      ┆         ┆          ┆   ┆                ┆ i64           ┆             ┆          │
╞════════╪══════╪═════════╪══════════╪═══╪════════════════╪═══════════════╪═════════════╪══════════╡
│ 1001   ┆ 1994 ┆ 1       ┆ 0.975312 ┆ … ┆ 1              ┆ 1             ┆ 0.270248    ┆ 0.003176 │
│ 1001   ┆ 1995 ┆ 1       ┆ 0.975312 ┆ … ┆ 1              ┆ 1             ┆ -0.038427   ┆ 0.048912 │
│ 1001   ┆ 1996 ┆ 1       ┆ 0.975312 ┆ … ┆ 1              ┆ 1             ┆ 0.161633    ┆ 0.058203 │
│ 1001   ┆ 1997 ┆ 1       ┆ 0.975312 ┆ … ┆ 1              ┆ 1             ┆ 0.056523    ┆ 0.044366 │
│ 1001   ┆ 1998 ┆ 1       ┆ 0.975312 ┆ … ┆ 1              ┆ 1             ┆ 0.034236    ┆ 0.047092 │
│ 1001   ┆ 1999 ┆ 1       ┆ 0.975312 ┆ … ┆ 1              ┆ 1             ┆ 0.048719    ┆ 0.00628  │
│ 1001   ┆ 2000 ┆ 1       ┆ 0.975312 ┆ … ┆ 1              ┆ 1             ┆ -0.034618   ┆ 0.018803 │
│ 1001   ┆ 2001 ┆ 1       ┆ 0.975312 ┆ … ┆ 1              ┆ 1             ┆ -0.010966   ┆ 0.068863 │
│ 1001   ┆ 2002 ┆ 1       ┆ 0.975312 ┆ … ┆ 1              ┆ 1             ┆ 0.121278    ┆ 0.060576 │
│ 1001   ┆ 2003 ┆ 1       ┆ 0.975312 ┆ … ┆ 1              ┆ 1             ┆ 0.202691    ┆ 0.024043 │
└────────┴──────┴─────────┴──────────┴───┴────────────────┴───────────────┴─────────────┴──────────┘