File: conversion.rst

package info (click to toggle)
pint-xarray 0.5.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky
  • size: 456 kB
  • sloc: python: 4,477; sh: 10; makefile: 4
file content (30 lines) | stat: -rw-r--r-- 744 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
.. currentmodule:: xarray

Converting units
================
.. ipython:: python
    :suppress:

    import xarray as xr

When working with :py:class:`Dataset` or :py:class:`DataArray` objects with
units, we frequently might want to convert the units. Suppose we have:

.. ipython::

    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
:py:meth:`Dataset.pint.to` and :py:meth:`DataArray.pint.to` methods:

.. ipython::

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

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