Skip to content

Latest commit

 

History

History
30 lines (20 loc) · 744 Bytes

conversion.rst

File metadata and controls

30 lines (20 loc) · 744 Bytes

xarray

Converting units

python

import xarray as xr

When working with :pyDataset or :pyDataArray objects with units, we frequently might want to convert the units. Suppose we have:

In [1]: ds = xr.Dataset(

...: {"a": ("x", [4, 8, 12, 16])}, coords={"u": ("x", [10, 20, 30, 40])} ...: ).pint.quantify({"a": "m", "u": "s"}) ...: ds

In [2]: da = ds.a

...: da

To convert the data to different units, we can use the :pyDataset.pint.to and :pyDataArray.pint.to methods:

In [3]: ds.pint.to(a="feet", u="ks")

In [4]: da.pint.to({da.name: "nautical_mile", "u": "ms"})