to_xarray#

pymc_marketing.clv.utils.to_xarray(customer_id, *arrays, dim='customer_id', extra_dims=None)[source]#

Convert arrays to xarray objects sharing a common dim (default “customer_id”).

The leading axis of every array is labelled dim and takes customer_id as its coordinate. Arrays with more than one axis are supported, but the names of their trailing axes must be supplied through extra_dims. Auto-generated names are deliberately not provided: they would make the unrelated trailing axes of two arrays returned by the same call align with each other.

For the same reason every array must be one-dimensional or carry the full set of trailing axes; a rank in between is rejected rather than guessed at, since extra_dims would not say which of its names the missing axis dropped. Arrays with different trailing axes need one call each.

Parameters:
customer_idarray_like

Coordinate values for dim. Must match the length of the leading axis of every array.

*arraysarray_like

Arrays to convert. Each must be one-dimensional or have 1 + len(extra_dims) dimensions.

dimstr, default “customer_id”

Name of the leading dimension.

extra_dimssequence of str, optional

Names of the trailing dimensions, e.g. ("channel",). Required as soon as an array is multidimensional. A bare string is rejected: it would be read as one name per character.

Returns:
xarray.DataArray or tuple of xarray.DataArray

One DataArray per entry in arrays, or a bare DataArray when a single array is passed.

Raises:
TypeError

If extra_dims is a string rather than a sequence of names.

ValueError

If extra_dims repeats a name or clashes with dim, or if an array is neither one-dimensional nor of dimension 1 + len(extra_dims).

Examples

frequency, recency = to_xarray(
    data["customer_id"], data["frequency"], data["recency"]
)
spend = to_xarray(data["customer_id"], channel_matrix, extra_dims=("channel",))

Trailing dimensions are named but not populated; attach their labels with assign_coords:

spend = to_xarray(
    data["customer_id"], channel_df, extra_dims=("channel",)
).assign_coords(channel=channel_df.columns)