Skip to content

NumPy Adapter - Category Helpers

Category encoding and decoding helpers used by the NumPy adapter. They were extracted into this sibling module so that causaliq_data.numpy stays within the module size limits described in the development policy.

The category to code mapping is captured when data is read in, and applied in reverse when a dataframe is requested, so round trips through NumPy.from_df() and NumPy.as_df() are unchanged.

Encoding Categories

category_codes

category_codes(
    df: DataFrame, max_category: int
) -> Dict[str, Optional[Tuple[str, ...]]]

Convert categorical columns to integer codes, in place.

Captures the code to value mapping as a tuple for each node, i.e. ('yes', 'no') implies integer code 0 maps to 'yes', 1 to 'no'.

Parameters:

  • df

    (DataFrame) –

    Dataframe whose categorical columns are converted.

  • max_category

    (int) –

    Maximum number of different category values.

Returns:

  • Dict[str, Optional[Tuple[str, ...]]] –

    Code to value mapping for each column.

Raises:

  • ValueError –

    If a column has too many category values.

Decoding Categories

restore_categories

restore_categories(df: DataFrame, categories: ndarray) -> None

Convert integer codes in a dataframe back to categories, in place.

Parameters:

  • df

    (DataFrame) –

    Dataframe whose integer codes are converted.

  • categories

    (ndarray) –

    Category values for each column.