moderndid.core.panel.make_balanced_panel#
- moderndid.core.panel.make_balanced_panel(data: Any, idname: str, tname: str) Any[source]#
Drop units not observed in every time period.
Many difference-in-differences estimators require a strictly balanced panel where every unit appears in every time period. When
allow_unbalanced_panel=False(the default inatt_gt), the preprocessing pipeline calls this function automatically. Calling it beforehand lets you inspect how many units will be dropped and decide whether balancing, gap-filling withfill_panel_gaps, or a flexible threshold viacomplete_datais more appropriate.- Parameters:
- Returns:
DataFrameBalanced panel in the same format as data.
See also
complete_dataKeep units observed in at least min_periods periods.
fill_panel_gapsInsert null rows instead of dropping units.
is_balanced_panelCheck whether the panel is already balanced.
Examples
In [1]: from moderndid import make_balanced_panel, load_favara_imbs ...: ...: df = load_favara_imbs() ...: balanced = make_balanced_panel(df, idname="county", tname="year") ...: print(f"Before: {df.shape[0]} rows, After: {balanced.shape[0]} rows") ...: Before: 12538 rows, After: 12516 rows