arviz_base.dataset_to_dataarray#
- arviz_base.dataset_to_dataarray(ds, sample_dims=None, labeller=None, add_coords=True, new_dim='label', label_type='flat')[source]#
Convert a Dataset to a stacked DataArray, using a labeller to set coordinate values.
- Parameters:
- ds
xarray.Dataset
Input data
- sample_dimssequence of hashable, optional
Dimensions that are present in all variables of ds and should be kept in the returned DataArray. All other variables will be stacked into new_dim.
- labeller
labeller
, optional Labeller instance with a make_label_flat or make_label_vert method that will be use to generate the coordinate values along new_dim.
- add_coordsbool,
default
True
Return multiple coordinate variables along new_dim. These will contain the newly generated labels, the stacked variable names, and stacked coordinate values.
- new_dimhashable,
default
“label” Name of the new dimension that is created from stacking variables and dimensions not in sample_dims.
- label_type{“flat”, “vert”},
default
“flat” if “flat”, then labeller.make_label_flat method is used to generate the labels and if “vert”, then labeller.make_label_vert method is used.
- ds
- Returns:
Examples
Convert the posterior group into a stacked and labelled dataarray:
import xarray as xr from arviz_base import load_arviz_data, dataset_to_dataarray xr.set_options(display_expand_data=False) idata = load_arviz_data("centered_eight") dataset_to_dataarray(idata.posterior.dataset)
<xarray.DataArray 'mu' (chain: 4, draw: 500, label: 10)> Size: 160kB 7.872 12.32 9.905 14.95 11.01 5.58 16.9 ... 6.849 1.86 7.936 6.762 1.295 4.461 Coordinates: * chain (chain) int64 32B 0 1 2 3 * draw (draw) int64 4kB 0 1 2 3 4 5 6 7 ... 493 494 495 496 497 498 499 * label (label) <U23 920B 'mu' 'theta[Choate]' ... 'tau' * variable (label) <U5 200B 'mu' 'theta' 'theta' ... 'theta' 'theta' 'tau' * school (label) object 80B nan 'Choate' 'Deerfield' ... 'Mt. Hermon' nan