xarray.Dataset.pint.to

Dataset.pint.to(units=None, **unit_kwargs)

convert the quantities in a Dataset

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 all the object’s data variables. If a dict-like, it maps variable names to unit names or pint.Unit objects.

  • **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

Dataset

Examples

>>> ds = xr.Dataset(
...     data_vars={
...         "a": ("x", np.linspace(0, 1, 5) * ureg.m),
...         "b": ("x", np.linspace(-1, 0, 5) * ureg.kg),
...     },
...     coords={"u": ("x", np.arange(5) * ureg.s)},
... )
>>> ds
<xarray.Dataset>
Dimensions:  (x: 5)
Coordinates:
    u        (x) int64 [s] 0 1 2 3 4
Dimensions without coordinates: x
Data variables:
    a        (x) float64 [m] 0.0 0.25 0.5 0.75 1.0
    b        (x) float64 [kg] -1.0 -0.75 -0.5 -0.25 0.0

Convert the data

>>> ds.pint.to({"a": "mm", "b": ureg.g})
<xarray.Dataset>
Dimensions:  (x: 5)
Coordinates:
    u        (x) int64 [s] 0 1 2 3 4
Dimensions without coordinates: x
Data variables:
    a        (x) float64 [mm] 0.0 250.0 500.0 750.0 1e+03
    b        (x) float64 [g] -1e+03 -750.0 -500.0 -250.0 0.0
>>> ds.pint.to(a=ureg.mm, b="g")
<xarray.Dataset>
Dimensions:  (x: 5)
Coordinates:
    u        (x) int64 [s] 0 1 2 3 4
Dimensions without coordinates: x
Data variables:
    a        (x) float64 [mm] 0.0 250.0 500.0 750.0 1e+03
    b        (x) float64 [g] -1e+03 -750.0 -500.0 -250.0 0.0

Convert coordinates

>>> ds.pint.to({"u": ureg.ms})
<xarray.Dataset>
Dimensions:  (x: 5)
Coordinates:
    u        (x) float64 [ms] 0.0 1e+03 2e+03 3e+03 4e+03
Dimensions without coordinates: x
Data variables:
    a        (x) float64 [m] 0.0 0.25 0.5 0.75 1.0
    b        (x) float64 [kg] -1.0 -0.75 -0.5 -0.25 0.0
>>> ds.pint.to(u="ms")
<xarray.Dataset>
Dimensions:  (x: 5)
Coordinates:
    u        (x) float64 [ms] 0.0 1e+03 2e+03 3e+03 4e+03
Dimensions without coordinates: x
Data variables:
    a        (x) float64 [m] 0.0 0.25 0.5 0.75 1.0
    b        (x) float64 [kg] -1.0 -0.75 -0.5 -0.25 0.0

Convert both simultaneously

>>> ds.pint.to(a=ureg.mm, b=ureg.g, u="ms")
<xarray.Dataset>
Dimensions:  (x: 5)
Coordinates:
    u        (x) float64 [ms] 0.0 1e+03 2e+03 3e+03 4e+03
Dimensions without coordinates: x
Data variables:
    a        (x) float64 [mm] 0.0 250.0 500.0 750.0 1e+03
    b        (x) float64 [g] -1e+03 -750.0 -500.0 -250.0 0.0
>>> ds.pint.to({"a": "mm", "b": "g", "u": ureg.ms})
<xarray.Dataset>
Dimensions:  (x: 5)
Coordinates:
    u        (x) float64 [ms] 0.0 1e+03 2e+03 3e+03 4e+03
Dimensions without coordinates: x
Data variables:
    a        (x) float64 [mm] 0.0 250.0 500.0 750.0 1e+03
    b        (x) float64 [g] -1e+03 -750.0 -500.0 -250.0 0.0

Convert homogeneous data

>>> ds = xr.Dataset(
...     data_vars={
...         "a": ("x", np.linspace(0, 1, 5) * ureg.kg),
...         "b": ("x", np.linspace(-1, 0, 5) * ureg.mg),
...     },
...     coords={"u": ("x", np.arange(5) * ureg.s)},
... )
>>> ds
<xarray.Dataset>
Dimensions:  (x: 5)
Coordinates:
    u        (x) int64 [s] 0 1 2 3 4
Dimensions without coordinates: x
Data variables:
    a        (x) float64 [kg] 0.0 0.25 0.5 0.75 1.0
    b        (x) float64 [mg] -1.0 -0.75 -0.5 -0.25 0.0
>>> ds.pint.to("g")
<xarray.Dataset>
Dimensions:  (x: 5)
Coordinates:
    u        (x) int64 [s] 0 1 2 3 4
Dimensions without coordinates: x
Data variables:
    a        (x) float64 [g] 0.0 250.0 500.0 750.0 1e+03
    b        (x) float64 [g] -0.001 -0.00075 -0.0005 -0.00025 0.0