xarray.DataArray.pint.quantify
- DataArray.pint.quantify(units=<object object>, unit_registry=None, **unit_kwargs)
Attach units to the DataArray.
Units can be specified as a pint.Unit or as a string, which will be parsed by the given unit registry. 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 with a different unit.
Note
Be aware that unless you’re using
daskthis will load the data into memory. To avoid that, consider converting todaskfirst (e.g. usingchunk).Note
Also note that datetime units (i.e. ones that match
{units} since {date}) in unit attributes will be ignored, to avoid interfering withxarray’s datetime encoding / decoding.- Parameters:
units (unit-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,
quantifywill try to read them fromDataArray.attrs['units']using pint’s parser. The"units"attribute will be removed from all variables except from dimension coordinates.unit_registry (
pint.UnitRegistry, optional) – Unit registry to be used for the units attached to this DataArray. If not given then a default registry will be created.**unit_kwargs – Keyword argument form of units.
- Returns:
quantified – DataArray whose wrapped array data will now be a Quantity array with the specified units.
- Return type:
Notes
"none"andNonecan be used to mark variables that should not be quantified.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
Don’t quantify the data:
>>> da = xr.DataArray( ... data=[0.4, 0.9], ... dims=["wavelength"], ... attrs={"units": "Hz"}, ... ) >>> da.pint.quantify(units=None) <xarray.DataArray (wavelength: 2)> Size: 16B array([0.4, 0.9]) Dimensions without coordinates: wavelength
Quantify with the same unit:
>>> q = da.pint.quantify() >>> q <xarray.DataArray (wavelength: 2)> Size: 16B <Quantity([0.4 0.9], 'hertz')> Dimensions without coordinates: wavelength >>> q.pint.quantify("Hz") <xarray.DataArray (wavelength: 2)> Size: 16B <Quantity([0.4 0.9], 'hertz')> Dimensions without coordinates: wavelength