long_to_wide_infill#

caf.toolkit.pandas_utils.long_to_wide_infill(matrix, *, infill=0, unstack_level=-1, check_totals=False, correct_cols=None, correct_ind=None)[source]#

Convert a DataFrame from long to wide format, infilling missing values.

Parameters:
  • matrix (Series) – The matrix, in long format (i.e. a Series), to convert to wide.

  • infill (Any) – The value to use to infill any missing cells in the wide DataFrame.

  • unstack_level (str | int) – The level to unstack from the index. This can either be an int, i.e. the ordinal level of the multiindex to unstack, or a string, i.e. the name of the index level to unstack. See pd.DataFrame.unstack().

  • check_totals (bool) – Whether to check if the totals are almost equal before and after the conversion.

  • correct_cols (list | None) – The correct columns for the resultant dataframe. If this is provided so must ‘correct_ind’ and vice versa.

  • correct_ind (list | None) – The correct index for the resultant dataframe. If this is provided so must ‘correct_cols’ and vice versa.

Returns:

A copy of df, in wide format, with index_col as the index, columns_col as the column names, and values_col as the values.

Return type:

wide_df

Raises:

TypeError: – If none of the values_col is not numeric and check_totals is True