Label Utils
Label Utilities - Labeling by Condition.
This module provides utilities to label groups in an Ibis table based on whether any items in the group meet a specified condition. It supports both binary labeling (contains/not_contains) and extended labeling (contains/mixed/not_contains).
Example use cases: - Tag transactions as containing a product, product category, or promotion - Tag customers as containing a product, product category, or promotion, or store_id - Tag a transaction as containing promo items, no promo items, or both on/off promo items
label_by_condition(table, condition, label_col=None, return_col='label_name', labeling_strategy='binary', contains_label='contains', not_contains_label='not_contains', mixed_label='mixed')
Labels groups in a table based on whether items in the group meet a condition.
This function groups a table by the specified label column and determines whether any items in each group meet the given condition. It supports two labeling strategies: - "binary": Labels groups as either "contains" or "not_contains" based on whether any item in the group meets the condition - "extended": Labels groups as "contains" (all items meet condition), "mixed" (some items meet condition), or "not_contains" (no items meet condition)
Parameters:
Name | Type | Description | Default |
---|---|---|---|
table |
Table
|
An ibis table to process. |
required |
condition |
BooleanColumn
|
Boolean expression representing the condition to evaluate. |
required |
label_col |
str | None
|
Column name to group by for labeling. If None, defaults to the setting column.customer_id. |
None
|
return_col |
str
|
Name of the column to add with the labels. Defaults to "label_name". |
'label_name'
|
labeling_strategy |
Literal['binary', 'extended']
|
Strategy for labeling groups. Defaults to "binary". |
'binary'
|
contains_label |
str | Value
|
Label for groups that contain items meeting the condition. Defaults to "contains". |
'contains'
|
not_contains_label |
str | Value
|
Label for groups that do not contain items meeting the condition. Defaults to "not_contains". |
'not_contains'
|
mixed_label |
str | Value
|
Label for groups with mixed results (only used with "extended" strategy). Defaults to "mixed". |
'mixed'
|
Returns:
Type | Description |
---|---|
Table
|
ibis.Table: An ibis table grouped by label_col with an added label column. |