Utilities¶
Cross-cutting helpers. pack_dataset stacks a Dataset's variables along a variable dim, so a carrier-agnostic operator (pipekit_array.einx, a KernelOp) can take them as one DataArray; unpack_dataset splits them back. scikit-learn on xarray is xrops.learn.
pack_dataset
¶
pack_dataset(ds: Dataset, variables: Sequence[str] | None = None, *, new_dim: str = 'variable') -> DataArray
Stack named variables of a Dataset along a new dim.
The variables must share dims and coords; the result is a single
DataArray with one extra new_dim axis whose coordinate is the
variable names.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
ds
|
Dataset
|
source Dataset. |
required |
variables
|
Sequence[str] | None
|
variable names to stack, in order. Defaults to all data variables (sorted by insertion order). |
None
|
new_dim
|
str
|
name of the new stacking dim. |
'variable'
|
Raises:
| Type | Description |
|---|---|
ValueError
|
if no variables are selected or |
Each variable's attrs are kept on the new_dim coordinate (as
variable_attrs), so :func:unpack_dataset restores them; the packed
array carries only the attrs every variable shares.
Inverse of :func:unpack_dataset.
Source code in packages/xrtoolz-ops/src/xrops/utils/_src/dataset.py
unpack_dataset
¶
Split a 'variable'-style dim back into a Dataset.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
da
|
DataArray
|
DataArray with a |
required |
dim
|
str
|
the stacking dim to split on. |
'variable'
|
Raises:
| Type | Description |
|---|---|
ValueError
|
if |
Inverse of :func:pack_dataset.