moderndid.core.panel.panel_to_wide#
- moderndid.core.panel.panel_to_wide(data: Any, idname: str, tname: str, separator: str = '_') Any[source]#
Pivot a long panel to wide format.
Reshapes the data so that each unit occupies a single row. Time-varying columns are spread into one column per period while time-invariant columns are kept as-is.
- Parameters:
- data
DataFrame Panel data in long format. Accepts any object implementing the Arrow PyCapsule Interface (
__arrow_c_stream__), including polars, pandas, pyarrow Table, and cudf DataFrames.- idname
str Unit identifier column.
- tname
str Time period column.
- separator
str String inserted between the variable name and time label in the wide column names. Default
"_".
- data
- Returns:
DataFrameWide-format DataFrame with one row per unit, in the same format as data.
See also
wide_to_panelInverse operation (wide to long).
Examples
In [1]: from moderndid import make_balanced_panel, panel_to_wide, 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") ...: wide.head(5) ...: Out[1]: shape: (5, 39) ┌────────┬─────────┬──────────┬────────────┬───┬────────────┬────────────┬────────────┬────────────┐ │ county ┆ state_n ┆ w1 ┆ Dl_vloans_ ┆ … ┆ Dl_hpi_200 ┆ Dl_hpi_200 ┆ Dl_hpi_200 ┆ Dl_hpi_200 │ │ --- ┆ --- ┆ --- ┆ b_1994 ┆ ┆ 2 ┆ 3 ┆ 4 ┆ 5 │ │ i64 ┆ i64 ┆ f64 ┆ --- ┆ ┆ --- ┆ --- ┆ --- ┆ --- │ │ ┆ ┆ ┆ f64 ┆ ┆ f64 ┆ f64 ┆ f64 ┆ f64 │ ╞════════╪═════════╪══════════╪════════════╪═══╪════════════╪════════════╪════════════╪════════════╡ │ 1001 ┆ 1 ┆ 0.975312 ┆ 0.270248 ┆ … ┆ 0.060576 ┆ 0.024043 ┆ 0.02454 ┆ 0.128786 │ │ 1007 ┆ 1 ┆ 0.975312 ┆ -0.101783 ┆ … ┆ 0.026326 ┆ -0.001457 ┆ 0.063339 ┆ 0.075017 │ │ 1009 ┆ 1 ┆ 0.975312 ┆ 0.464161 ┆ … ┆ 0.029511 ┆ 0.000463 ┆ 0.061479 ┆ 0.075183 │ │ 1015 ┆ 1 ┆ 0.975312 ┆ 0.280577 ┆ … ┆ 0.056214 ┆ 0.052125 ┆ 0.019661 ┆ 0.017381 │ │ 1021 ┆ 1 ┆ 0.975312 ┆ -0.398833 ┆ … ┆ 0.02722 ┆ -0.002381 ┆ 0.060601 ┆ 0.073253 │ └────────┴─────────┴──────────┴────────────┴───┴────────────┴────────────┴────────────┴────────────┘