xarray.Dataset.astropy.dequantify¶
- Dataset.astropy.dequantify(format=None)¶
Convert units from the Dataset to string attributes.
Will replace
.attrs['units']on each variable with a string representation of theastropy.units.Unitinstance.- Parameters:
format (
str, default:None) – The format specification (as accepted by astropy’s unit formatter) used for the string representations. IfNone, the default (astropy.units.format.Unicode) is used instead.- Returns:
dequantified – Dataset whose data variables are unitless, and of the type that was previously wrapped by
astropy.Quantity.- Return type:
See also
- String Representations of Units and Quantities
astropy’s string formatting guide
Examples
>>> ds = xr.Dataset({"a": ("x", [0, 1]), "b": ("y", [2, 3, 4])}) >>> q = ds.astropy.quantify({"a": "m / s", "b": "s"}) >>> q <xarray.Dataset> Size: 40B Dimensions: (x: 2, y: 3) Dimensions without coordinates: x, y Data variables: a (x) float64 16B [m s⁻¹] 0.0 1.0 b (y) float64 24B [s] 2.0 3.0 4.0
>>> d = q.astropy.dequantify(format="unicode") >>> d.a <xarray.DataArray 'a' (x: 2)> Size: 16B array([0., 1.]) Dimensions without coordinates: x .. attribute:: units
m s⁻¹
>>> d.b <xarray.DataArray 'b' (y: 3)> Size: 24B array([2., 3., 4.]) Dimensions without coordinates: y .. attribute:: units
s
>>> d = q.astropy.dequantify(format="generic") >>> d.a <xarray.DataArray 'a' (x: 2)> Size: 16B array([0., 1.]) Dimensions without coordinates: x .. attribute:: units
m / s
>>> d.b <xarray.DataArray 'b' (y: 3)> Size: 24B array([2., 3., 4.]) Dimensions without coordinates: y .. attribute:: units
s
>>> d = q.astropy.dequantify(format="latex") >>> d.a <xarray.DataArray 'a' (x: 2)> Size: 16B array([0., 1.]) Dimensions without coordinates: x .. attribute:: units
$mathrm{frac{m}{s}}$
>>> d.b <xarray.DataArray 'b' (y: 3)> Size: 24B array([2., 3., 4.]) Dimensions without coordinates: y .. attribute:: units
$mathrm{s}$