Converting units

When working with Dataset or DataArray 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
   ...: 
Out[1]: 
<xarray.Dataset>
Dimensions:  (x: 4)
Coordinates:
    u        (x) int64 [s] 10 20 30 40
Dimensions without coordinates: x
Data variables:
    a        (x) int64 [m] 4 8 12 16

In [2]: da = ds.a
   ...: da
   ...: 
Out[2]: 
<xarray.DataArray 'a' (x: 4)>
<Quantity([ 4  8 12 16], 'meter')>
Coordinates:
    u        (x) int64 [s] 10 20 30 40
Dimensions without coordinates: x

To convert the data to different units, we can use the Dataset.pint.to() and DataArray.pint.to() methods:

In [3]: ds.pint.to(a="feet", u="ks")
Out[3]: 
<xarray.Dataset>
Dimensions:  (x: 4)
Coordinates:
    u        (x) float64 [ks] 0.01 0.02 0.03 0.04
Dimensions without coordinates: x
Data variables:
    a        (x) float64 [ft] 13.12 26.25 39.37 52.49

In [4]: da.pint.to({da.name: "nautical_mile", "u": "ms"})
Out[4]: 
<xarray.DataArray 'a' (x: 4)>
<Quantity([0.00215983 0.00431965 0.00647948 0.00863931], 'nautical_mile')>
Coordinates:
    u        (x) float64 [ms] 1e+04 2e+04 3e+04 4e+04
Dimensions without coordinates: x