xarray.DataArray.pint.to
- DataArray.pint.to(units=None, **unit_kwargs)
convert the quantities in a DataArray
- Parameters:
units (unit-like or mapping of hashable to unit-like, optional) – The units to convert to. If a unit name or
pint.Unit
object, convert the DataArray’s data. If a dict-like, it has to map a variable name to a unit name orpint.Unit
object.**unit_kwargs – The kwargs form of
units
. Can only be used for variable names that are strings and valid python identifiers.
- Returns:
object – A new object with converted units.
- Return type:
Examples
>>> da = xr.DataArray( ... data=np.linspace(0, 1, 5) * ureg.m, ... coords={"u": ("x", np.arange(5) * ureg.s)}, ... dims="x", ... name="arr", ... ) >>> da <xarray.DataArray 'arr' (x: 5)> Size: 40B <Quantity([0. 0.25 0.5 0.75 1. ], 'meter')> Coordinates: u (x) int64 40B [s] 0 1 2 3 4 Dimensions without coordinates: x
Convert the data
>>> da.pint.to("mm") <xarray.DataArray 'arr' (x: 5)> Size: 40B <Quantity([ 0. 250. 500. 750. 1000.], 'millimeter')> Coordinates: u (x) int64 40B [s] 0 1 2 3 4 Dimensions without coordinates: x >>> da.pint.to(ureg.mm) <xarray.DataArray 'arr' (x: 5)> Size: 40B <Quantity([ 0. 250. 500. 750. 1000.], 'millimeter')> Coordinates: u (x) int64 40B [s] 0 1 2 3 4 Dimensions without coordinates: x >>> da.pint.to({da.name: "mm"}) <xarray.DataArray 'arr' (x: 5)> Size: 40B <Quantity([ 0. 250. 500. 750. 1000.], 'millimeter')> Coordinates: u (x) int64 40B [s] 0 1 2 3 4 Dimensions without coordinates: x
Convert coordinates
>>> da.pint.to({"u": ureg.ms}) <xarray.DataArray 'arr' (x: 5)> Size: 40B <Quantity([0. 0.25 0.5 0.75 1. ], 'meter')> Coordinates: u (x) float64 40B [ms] 0.0 1e+03 2e+03 3e+03 4e+03 Dimensions without coordinates: x >>> da.pint.to(u="ms") <xarray.DataArray 'arr' (x: 5)> Size: 40B <Quantity([0. 0.25 0.5 0.75 1. ], 'meter')> Coordinates: u (x) float64 40B [ms] 0.0 1e+03 2e+03 3e+03 4e+03 Dimensions without coordinates: x
Convert both simultaneously
>>> da.pint.to("mm", u="ms") <xarray.DataArray 'arr' (x: 5)> Size: 40B <Quantity([ 0. 250. 500. 750. 1000.], 'millimeter')> Coordinates: u (x) float64 40B [ms] 0.0 1e+03 2e+03 3e+03 4e+03 Dimensions without coordinates: x >>> da.pint.to({"arr": ureg.mm, "u": ureg.ms}) <xarray.DataArray 'arr' (x: 5)> Size: 40B <Quantity([ 0. 250. 500. 750. 1000.], 'millimeter')> Coordinates: u (x) float64 40B [ms] 0.0 1e+03 2e+03 3e+03 4e+03 Dimensions without coordinates: x >>> da.pint.to(arr="mm", u="ms") <xarray.DataArray 'arr' (x: 5)> Size: 40B <Quantity([ 0. 250. 500. 750. 1000.], 'millimeter')> Coordinates: u (x) float64 40B [ms] 0.0 1e+03 2e+03 3e+03 4e+03 Dimensions without coordinates: x