aggregate_idata_time#

pymc_marketing.data.idata.utils.aggregate_idata_time(idata, period, method='sum')[source]#

Aggregate DataTree over time periods.

Parameters:
  • idata (DataTree) – DataTree to aggregate

  • period (Literal['original', 'weekly', 'monthly', 'quarterly', 'yearly', 'all_time']) – Time period to aggregate to. Use “original” for no aggregation (returns unchanged), “all_time” to aggregate over the entire time dimension (removes the date dimension).

  • method (Literal['sum', 'mean']) – Aggregation method

Return type:

xr.DataTree

Returns:

New DataTree with aggregated groups (or unchanged if period=”original”). Its root attrs record the aggregation as time_aggregation=period so that consumers of per-date quantities can tell the data apart from the original dates.

Notes

Each variable is reduced as it is, so a variable without a date dim passes through unchanged. A time-invariant MMM intercept is such a variable although the model adds it in every period: summing MMM contributions with this function counts it once for the whole window. Use aggregate_time(), which broadcasts it over date first.

Examples

>>> original = aggregate_idata_time(idata, "original")  # No aggregation
>>> monthly = aggregate_idata_time(idata, "monthly", method="sum")
>>> total = aggregate_idata_time(idata, "all_time", method="sum")