moderndid.to_df#
- moderndid.to_df(result: Any, **kwargs: Any) DataFrame[source]#
Convert any ModernDiD result object to a polars DataFrame.
- Parameters:
- result
Any A ModernDiD result object. Supported types:
- **kwargs
Additional arguments passed to the underlying converter. For example,
effect_type="acrt"for DoseResult.
- result
- Returns:
polars.DataFrameDataFrame with columns appropriate to the result type.
Examples
Convert group-time ATT results from
att_gtinto a tidy DataFrame with one row per (group, time) cell:In [1]: from moderndid import att_gt, aggte, load_mpdta, to_df ...: ...: df = load_mpdta() ...: result = att_gt( ...: data=df, ...: yname="lemp", ...: tname="year", ...: gname="first.treat", ...: idname="countyreal", ...: est_method="dr", ...: boot=False, ...: ) ...: print(to_df(result).head()) ...: shape: (5, 7) ┌────────┬──────┬───────────┬──────────┬───────────┬───────────┬──────────────────┐ │ group ┆ time ┆ att ┆ se ┆ ci_lower ┆ ci_upper ┆ treatment_status │ │ --- ┆ --- ┆ --- ┆ --- ┆ --- ┆ --- ┆ --- │ │ f64 ┆ i64 ┆ f64 ┆ f64 ┆ f64 ┆ f64 ┆ str │ ╞════════╪══════╪═══════════╪══════════╪═══════════╪═══════════╪══════════════════╡ │ 2004.0 ┆ 2004 ┆ -0.010503 ┆ 0.023251 ┆ -0.056074 ┆ 0.035068 ┆ Post │ │ 2004.0 ┆ 2005 ┆ -0.070423 ┆ 0.030985 ┆ -0.131152 ┆ -0.009694 ┆ Post │ │ 2004.0 ┆ 2006 ┆ -0.137259 ┆ 0.036436 ┆ -0.208671 ┆ -0.065846 ┆ Post │ │ 2004.0 ┆ 2007 ┆ -0.100811 ┆ 0.034359 ┆ -0.168154 ┆ -0.033469 ┆ Post │ │ 2006.0 ┆ 2004 ┆ 0.00652 ┆ 0.023327 ┆ -0.0392 ┆ 0.05224 ┆ Pre │ └────────┴──────┴───────────┴──────────┴───────────┴───────────┴──────────────────┘
Aggregated event-study results work the same way:
In [2]: agg = aggte(result, type="dynamic") ...: print(to_df(agg)) ...: shape: (7, 6) ┌────────────┬───────────┬──────────┬───────────┬───────────┬──────────────────┐ │ event_time ┆ att ┆ se ┆ ci_lower ┆ ci_upper ┆ treatment_status │ │ --- ┆ --- ┆ --- ┆ --- ┆ --- ┆ --- │ │ f64 ┆ f64 ┆ f64 ┆ f64 ┆ f64 ┆ str │ ╞════════════╪═══════════╪══════════╪═══════════╪═══════════╪══════════════════╡ │ -3.0 ┆ 0.030507 ┆ 0.015034 ┆ -0.007607 ┆ 0.06862 ┆ Pre │ │ -2.0 ┆ -0.000563 ┆ 0.013292 ┆ -0.034261 ┆ 0.033134 ┆ Pre │ │ -1.0 ┆ -0.024459 ┆ 0.014236 ┆ -0.060551 ┆ 0.011634 ┆ Pre │ │ 0.0 ┆ -0.019932 ┆ 0.011826 ┆ -0.049915 ┆ 0.010051 ┆ Post │ │ 1.0 ┆ -0.050957 ┆ 0.016893 ┆ -0.093786 ┆ -0.008128 ┆ Post │ │ 2.0 ┆ -0.137259 ┆ 0.036436 ┆ -0.229632 ┆ -0.044885 ┆ Post │ │ 3.0 ┆ -0.100811 ┆ 0.034359 ┆ -0.18792 ┆ -0.013702 ┆ Post │ └────────────┴───────────┴──────────┴───────────┴───────────┴──────────────────┘