to_pandas_with_time_index#
- causalpy.input_data.to_pandas_with_time_index(data, time_column=None, *, argument_name='data')[source]#
Convert a dataframe-like input and put its time axis on the index.
Experiments that compare observations against a
treatment_timeneed a time axis. Pandas callers have historically supplied it as the dataframe index. Dataframes from other libraries have no index, so those callers must name the column that holds the time axis.- Parameters:
data (
NativeDataFrame) – Any eager dataframe supported by Narwhals.time_column (
str|None) – Column holding the time axis. When given, it becomes the index. When None, the pandas index ofdatais used, which requires a pandas input.argument_name (
str) – Name of the calling argument, used in error messages.
- Returns:
A pandas dataframe indexed by the time axis.
- Return type:
- Raises:
DataException – If
time_columnis missing from the data, if a non-pandas input arrives without atime_column, iftime_columnis given for a dataframe that already carries a meaningful index, or if the resulting time axis has duplicates or is not sorted.
Examples
>>> import pandas as pd >>> from causalpy.input_data import to_pandas_with_time_index >>> frame = pd.DataFrame({"t": [1, 2], "y": [3, 4]}) >>> result = to_pandas_with_time_index(frame, time_column="t") >>> result.index.name 't' >>> result.index.tolist() [1, 2] >>> result.columns.tolist() ['y']