xarray.DataArray.pr.quantify#
- DataArray.pr.quantify(**kwargs)#
Attaches units to the DataArray.
Units can be specified as a :py:class`pint.Unit` or as a string. If no units are specified then the units will be parsed from the ‘units’ entry of the DataArray’s .attrs. Will raise a ValueError if the DataArray already contains a unit-aware array.
This function is a wrapper for pint_xarrays function with the same name, which uses the primap2 unit registry. Calling
da.pr.quantify()
is therefore equivalent to callingda.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:
- unitsunit-like or mapping of hashable to unit-like, optional
Physical units to use for this DataArray. If a str or pint.Unit, will be used as the DataArray’s units. If a dict-like, it should map a variable name to the desired unit (use the DataArray’s name to refer to its data). If not provided, will try to read them from
DataArray.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:
- quantifiedDataArray
DataArray whose wrapped array data will now be a Quantity array with the specified units.
Examples
>>> da = xr.DataArray( ... data=[0.4, 0.9, 1.7, 4.8, 3.2, 9.1], ... dims=["wavelength"], ... coords={"wavelength": [1e-4, 2e-4, 4e-4, 6e-4, 1e-3, 2e-3]}, ... ) >>> da.pint.quantify(units="Hz") <xarray.DataArray (wavelength: 6)> Size: 48B <Quantity([0.4 0.9 1.7 4.8 3.2 9.1], 'hertz')> Coordinates: * wavelength (wavelength) float64 48B 0.0001 0.0002 0.0004 0.0006 0.001 0.002