|
| 1 | +[](https://github.com/xarray-contrib/pint-xarray/actions?query=branch%3Amaster) |
| 2 | +[](https://codecov.io/gh/xarray-contrib/pint-xarray) |
| 3 | +[](https://pint-xarray.readthedocs.io) |
| 4 | +[](https://pypi.org/project/pint-xarray) |
| 5 | +[](https://github.com/python/black) |
| 6 | +[](https://github.com/conda-forge/pint-xarray-feedstock) |
| 7 | + |
1 | 8 | # pint-xarray
|
2 |
| -Interface for using pint in xarray, providing convenience accessors, as [envisioned here](https://github.com/hgrecco/pint/issues/849). |
| 9 | + |
| 10 | +A convenience wrapper for using [pint](https://pint.readthedocs.io) with |
| 11 | +[xarray](https://xarray.pydata.org). |
| 12 | + |
| 13 | +## Usage |
| 14 | + |
| 15 | +To convert the variables of a `Dataset` to quantities: |
| 16 | +```python |
| 17 | +In [1]: import pint_xarray |
| 18 | + ...: import xarray as xr |
| 19 | + |
| 20 | +In [2]: ds = xr.Dataset({"a": ("x", [0, 1, 2]), "b": ("y", [-3, 5, 1], {"units": "m"})}) |
| 21 | + ...: ds |
| 22 | +Out[2]: |
| 23 | +<xarray.Dataset> |
| 24 | +Dimensions: (x: 3, y: 3) |
| 25 | +Dimensions without coordinates: x, y |
| 26 | +Data variables: |
| 27 | + a (x) int64 0 1 2 |
| 28 | + b (y) int64 -3 5 1 |
| 29 | + |
| 30 | +In [3]: q = ds.pint.quantify(a="s") |
| 31 | + ...: q |
| 32 | +Out[3]: |
| 33 | +<xarray.Dataset> |
| 34 | +Dimensions: (x: 3, y: 3) |
| 35 | +Dimensions without coordinates: x, y |
| 36 | +Data variables: |
| 37 | + a (x) int64 [s] 0 1 2 |
| 38 | + b (y) int64 [m] -3 5 1 |
| 39 | +``` |
| 40 | +to convert to different units: |
| 41 | +```python |
| 42 | +In [4]: c = q.pint.to({"a": "ms", "b": "km"}) |
| 43 | + ...: c |
| 44 | +Out[4]: |
| 45 | +<xarray.Dataset> |
| 46 | +Dimensions: (x: 3, y: 3) |
| 47 | +Dimensions without coordinates: x, y |
| 48 | +Data variables: |
| 49 | + a (x) float64 [ms] 0.0 1e+03 2e+03 |
| 50 | + b (y) float64 [km] -0.003 0.005 0.001 |
| 51 | +``` |
| 52 | +to convert back to non-quantities: |
| 53 | +```python |
| 54 | +In [5]: d = c.pint.dequantify() |
| 55 | + ...: d |
| 56 | +Out[5]: |
| 57 | +<xarray.Dataset> |
| 58 | +Dimensions: (x: 3, y: 3) |
| 59 | +Dimensions without coordinates: x, y |
| 60 | +Data variables: |
| 61 | + a (x) float64 0.0 1e+03 2e+03 |
| 62 | + b (y) float64 -0.003 0.005 0.001 |
| 63 | +``` |
| 64 | + |
| 65 | +For more, see the [documentation](https://pint-xarray.readthedocs.io) |
0 commit comments