causalis.data_contracts.panel_data_did¶
Module Contents¶
Classes¶
Validated long-format panel contract for staggered-adoption DID. |
Data¶
API¶
- causalis.data_contracts.panel_data_did.TimeLike¶
None
- causalis.data_contracts.panel_data_did.ComparisonGroup¶
None
- class causalis.data_contracts.panel_data_did.PanelDataDID(/, **data: Any)¶
Bases:
pydantic.BaseModelValidated long-format panel contract for staggered-adoption DID.
Required fields
df : pandas.DataFrame Long-format panel data. y : str Outcome column name in
df. unit_col : str Unit identifier column name indf. time_col : str Calendar time column name indf. Preferred input format ispandas.Periodvalues with a regular frequency (for example monthlyPeriod['M']). Datetime/string values are accepted only when a regular frequency can be inferred. treated_time : str Binary period-level treatment column indf(0/1 or False/True). Treatment is required to be absorbing: once a unit is treated, every later observed row for that unit must remain treated. covariates : sequence of str, optional Optional numeric covariate column names indf. The input aliascovariantsis accepted for convenience. cluster_col : str, optional Optional cluster identifier column name indf. The input aliasclusteris accepted for convenience.unit_colis valid for unit-level clustering. Estimators that form unit-level influence functions require non-unit cluster columns to be stable within unit;time_colis therefore valid for the contract but not for those estimators.Notes
Extra keyword arguments are rejected. The contract derives
treated_units,control_units(never-treated units),cohorts, per-unit first treatment dates, andtime_freqfrom the input data.treatment_startis kept as a compatibility alias for the earliest treatment cohort; staggered estimators should usecohortsandfirst_treatment_by_unitinstead. This contract is intended for Callaway & Sant’Anna-style staggered difference-in-differences designs. It validates cohort support for at least one estimableATT(g,t)cell using not-yet-treated or never-treated comparison units and exposes a full support table viaatt_gt_cells. The model stores a validated internal dataframe snapshot used by all contract methods; mutating the publicdfattribute after construction does not affect validated contract behavior. Outcomeymust not contain null/NaN values. Represent missing panel periods by omitting unit-time rows, not by keeping rows withNaNoutcome. For fiscal quarter/year semantics, passtime_colexplicitly aspandas.Periodwith the desired fiscal frequency.Initialization
Create a new model by parsing and validating input data from keyword arguments.
Raises [
ValidationError][pydantic_core.ValidationError] if the input data cannot be validated to form a valid model.selfis explicitly positional-only to allowselfas a field name.- model_config¶
‘ConfigDict(…)’
- df: pandas.DataFrame¶
‘Field(…)’
- y: str¶
‘Field(…)’
- unit_col: str¶
‘Field(…)’
- time_col: str¶
‘Field(…)’
- treated_time: str¶
‘Field(…)’
- covariates: tuple[str, ...]¶
‘Field(…)’
- cluster_col: Optional[str]¶
‘Field(…)’
- property treated_units: Sequence[Hashable]¶
- property control_units: Sequence[Hashable]¶
- property never_treated_units: Sequence[Hashable]¶
Units that are never treated in the observed panel.
- property first_treatment_by_unit: dict[Hashable, Optional[pandas.Period]]¶
Map every unit to its first treatment period, or
Noneif never treated.
- property cohort_by_unit: dict[Hashable, pandas.Period]¶
Map ever-treated units to their first treatment cohort.
- property cohorts: Sequence[pandas.Period]¶
Sorted first-treatment periods among ever-treated units.
- property treatment_start: pandas.Period¶
- property latest_treatment_start: pandas.Period¶
- property time_freq: str¶
- property n_pre_periods: int¶
- property n_post_periods: int¶
- property last_post_period: pandas.Period¶
- property design_type: Literal[canonical_2x2, simultaneous_adoption, staggered_adoption]¶
- property has_covariates: bool¶
- property has_cluster: bool¶
- df_analysis() pandas.DataFrame¶
- covariate_frame() pandas.DataFrame¶
Return a copy of the validated covariate design columns.
- cluster_series() pandas.Series¶
Return a copy of the validated cluster identifier column.
- cohort_units(cohort: causalis.data_contracts.panel_data_did.TimeLike) Sequence[Hashable]¶
Return units first treated in the requested cohort.
- not_yet_treated_units(time: causalis.data_contracts.panel_data_did.TimeLike, *, include_never: bool = True) Sequence[Hashable]¶
Return units untreated at
timebecause they adopt later or never adopt.
- comparison_units(cohort: causalis.data_contracts.panel_data_did.TimeLike, time: causalis.data_contracts.panel_data_did.TimeLike, *, control_group: causalis.data_contracts.panel_data_did.ComparisonGroup = 'not_yet_or_never') Sequence[Hashable]¶
Return valid comparison units for a Callaway-Sant’Anna
ATT(g,t)cell.
- df_for_did(*, treated_group_col: str = 'treated_group', post_col: str = 'post', cohort_col: str = 'cohort', event_time_col: str = 'event_time') pandas.DataFrame¶
Return analysis data with derived staggered-DID cohort and event-time columns.
- pre_times(cohort: Optional[causalis.data_contracts.panel_data_did.TimeLike] = None) Sequence[pandas.Period]¶
- post_times(cohort: Optional[causalis.data_contracts.panel_data_did.TimeLike] = None) Sequence[pandas.Period]¶
- analysis_times() Sequence[pandas.Period]¶
- time_to_index() dict[pandas.Period, int]¶
- treatment_start_idx(cohort: Optional[causalis.data_contracts.panel_data_did.TimeLike] = None) int¶
- att_gt_cells(*, control_group: causalis.data_contracts.panel_data_did.ComparisonGroup = 'not_yet_or_never', anticipation: int = 0, base_period: Literal[universal, varying] = 'universal', include_pre_periods: bool = False, include_unsupported: bool = False) pandas.DataFrame¶
Return Callaway-Sant’Anna
ATT(g,t)support under an explicit policy.
- cell_counts() pandas.DataFrame¶
Return ever-treated/never-treated row counts by analysis period and own-treatment status.
- __repr__() str¶