Skip to content

panel_aware_prefix

yohou.utils.panel.panel_aware_prefix(col, prefix)

Add a prefix to a column name while preserving the panel group prefix.

For panel data columns (<GROUP>__<SERIES>), the prefix is inserted after the group separator: <GROUP>__<PREFIX>_<SERIES>.

For global columns, the prefix is prepended: <PREFIX>_<COLUMN>.

Parameters

Name Type Description Default
col str

Column name to prefix.

required
prefix str

Prefix string to add (joined with _).

required

Returns

Type Description
str

Column name with prefix added in the panel-safe position.

Examples

>>> panel_aware_prefix("sales", "boxcox")
'boxcox_sales'
>>> panel_aware_prefix("store_1__sales", "boxcox")
'store_1__boxcox_sales'

See Also

Source Code

Show/Hide source
def panel_aware_prefix(col: str, prefix: str) -> str:
    """Add a prefix to a column name while preserving the panel group prefix.

    For panel data columns (``<GROUP>__<SERIES>``), the prefix is inserted
    after the group separator: ``<GROUP>__<PREFIX>_<SERIES>``.

    For global columns, the prefix is prepended: ``<PREFIX>_<COLUMN>``.

    Parameters
    ----------
    col : str
        Column name to prefix.
    prefix : str
        Prefix string to add (joined with ``_``).

    Returns
    -------
    str
        Column name with prefix added in the panel-safe position.

    Examples
    --------
    >>> panel_aware_prefix("sales", "boxcox")
    'boxcox_sales'
    >>> panel_aware_prefix("store_1__sales", "boxcox")
    'store_1__boxcox_sales'

    See Also
    --------
    - [`panel_aware_rename`][yohou.utils.panel.panel_aware_rename] : General-purpose panel-aware rename utility.

    """
    return panel_aware_rename(col, lambda member: f"{prefix}_{member}")