xarray.Dataset.pr.quantify#
- Dataset.pr.quantify(units=None, **unit_kwargs) Dataset #
Attaches units to each variable in the Dataset.
Units can be specified as a
pint.Unit
or as a string. If no units are specified then the units will be parsed from the"units"
entry of the Dataset variable’s.attrs
. Will raise a ValueError if any of the variables already contain a unit-aware array.This function is a wrapper for pint_xarrays function with the same name, which uses the primap2 unit registry. Calling
ds.pr.quantify()
is therefore equivalent to callingds.pint.quantify(unit_registry=primap2.ureg)
Note
Be aware that unless you’re using
dask
this will load the data into memory. To avoid that, consider converting todask
first (e.g. usingchunk
).As units in dimension coordinates are not supported until
xarray
changes the way it implements indexes, these units will be set as attributes.- Parameters:
- unitsmapping of hashable to unit-like, optional
Physical units to use for particular DataArrays in this Dataset. It should map variable names to units (unit names or
pint.Unit
objects). If not provided, will try to read them fromDataset[var].attrs['units']
using pint’s parser. The"units"
attribute will be removed from all variables except from dimension coordinates.- **unit_kwargs
Keyword argument form of
units
.
- Returns:
- quantifiedDataset
The variables in quantified will now contain Quantity arrays with units.
Examples
>>> import xarray as xr >>> import primap2 >>> ds = xr.Dataset( ... {"a": ("x", [0, 3, 2], {"units": "m"}), "b": ("x", [5, -2, 1])}, ... coords={"x": [0, 1, 2], "u": ("x", [-1, 0, 1], {"units": "s"})}, ... ) >>> ds <xarray.Dataset> Size: ... Dimensions: (x: 3) Coordinates: * x (x) int... 0 1 2 u (x) int... -1 0 1 Data variables: a (x) int... 0 3 2 b (x) int... 5 -2 1
>>> ds.pr.quantify() <xarray.Dataset> Size: ... Dimensions: (x: 3) Coordinates: * x (x) int... 0 1 2 u (x) int... [s] -1 0 1 Data variables: a (x) int... [m] 0 3 2 b (x) int... 5 -2 1 >>> ds.pr.quantify({"b": "dm"}) <xarray.Dataset> Size: ... Dimensions: (x: 3) Coordinates: * x (x) int... 0 1 2 u (x) int... [s] -1 0 1 Data variables: a (x) int... [m] 0 3 2 b (x) int... [dm] 5 -2 1