scipp.compat.xarray_compat.from_xarray#

scipp.compat.xarray_compat.from_xarray(obj)#

Convert an xarray object to the corresponding scipp object. Attributes named “units” are used to set the units of the Variables. All other DataArray attributes are kept, but attributes of Variables, Coordinates and Datasets are dropped.

Parameters:

obj (Variable | DataArray | Dataset) – The xarray object to convert.

Returns:

VariableLike – The converted scipp object.

See also

scipp.compat.to_xarray

Examples

Convert an xarray DataArray to a scipp DataArray:

>>> import scipp as sc
>>> import xarray as xr
>>> xr_da = xr.DataArray(
...     [[1, 2, 3], [4, 5, 6]],
...     dims=['x', 'y'],
...     coords={'x': [10, 20], 'y': [0.1, 0.2, 0.3]},
...     attrs={'units': 'm'}
... )
>>> sc.compat.from_xarray(xr_da)
<scipp.DataArray>
Dimensions: Sizes[x:2, y:3, ]
Coordinates:
* x                           int64  [dimensionless]  (x)  [10, 20]
* y                         float64  [dimensionless]  (y)  [0.1, 0.2, 0.3]
Data:
                              int64              [m]  (x, y)  [1, 2, ..., 5, 6]