data_conversion#

Data conversion utilities for MMM.

Normalises various input types (pd.DataFrame, xr.Dataset, xr.DataArray) into the canonical xr.Dataset format that the MMM class uses internally.

Public vs. internal variable names#

Public name

Internal name

Description

media

_channel

Media channel spend data

target

_target

Response/target variable

_control

_control

Control variable data

The names channel and control cannot be used as data variable names because xarray promotes them to dimension coordinates when the variable and dimension share a name. Use media instead of channel, and use _control for control data.

Examples#

import xarray as xr
import pandas as pd
from pymc_marketing.mmm.data_conversion import to_mmm_dataset

# From a DataFrame
df = pd.DataFrame(
    {
        "date": pd.date_range("2025-01-01", periods=10, freq="W"),
        "tv": [1, 2, 3, 4, 5, 6, 7, 8, 9, 10],
        "digital": [2, 3, 4, 5, 6, 7, 8, 9, 10, 11],
    }
)
y = pd.Series([10, 20, 30, 40, 50, 60, 70, 80, 90, 100], name="sales")

ds = to_mmm_dataset(
    df,
    y,
    date_column="date",
    channel_columns=["tv", "digital"],
    target_column="sales",
)
# ds has variables _channel and _target

Functions

to_mmm_dataset(X[, y, dims, ...])

Normalise X (and optionally y) to a single canonical xr.Dataset.