https://github.com/xarray-contrib/pint-xarray
Interface for using pint with xarray, providing convenience accessors
https://github.com/xarray-contrib/pint-xarray
accessor pint xarray
Last synced: 5 days ago
JSON representation
Interface for using pint with xarray, providing convenience accessors
- Host: GitHub
- URL: https://github.com/xarray-contrib/pint-xarray
- Owner: xarray-contrib
- License: apache-2.0
- Created: 2020-04-07T10:21:19.000Z (about 5 years ago)
- Default Branch: main
- Last Pushed: 2025-03-31T18:43:37.000Z (14 days ago)
- Last Synced: 2025-04-02T03:16:20.526Z (13 days ago)
- Topics: accessor, pint, xarray
- Language: Python
- Homepage: https://pint-xarray.readthedocs.io/en/latest/
- Size: 300 KB
- Stars: 109
- Watchers: 7
- Forks: 12
- Open Issues: 25
-
Metadata Files:
- Readme: README.md
- Contributing: docs/contributing.rst
- License: LICENSE
Awesome Lists containing this project
- awesome-meteo - pint-xarray
README
[](https://github.com/xarray-contrib/pint-xarray/actions?query=branch%3Amain)
[](https://codecov.io/gh/xarray-contrib/pint-xarray)
[](https://pint-xarray.readthedocs.io)
[](https://pypi.org/project/pint-xarray)
[](https://github.com/python/black)
[](https://github.com/conda-forge/pint-xarray-feedstock)# pint-xarray
A convenience wrapper for using [pint](https://pint.readthedocs.io) with
[xarray](https://xarray.pydata.org).## Usage
To convert the variables of a `Dataset` to quantities:
```python
In [1]: import pint_xarray
...: import xarray as xrIn [2]: ds = xr.Dataset({"a": ("x", [0, 1, 2]), "b": ("y", [-3, 5, 1], {"units": "m"})})
...: ds
Out[2]:Dimensions: (x: 3, y: 3)
Dimensions without coordinates: x, y
Data variables:
a (x) int64 0 1 2
b (y) int64 -3 5 1In [3]: q = ds.pint.quantify(a="s")
...: q
Out[3]:Dimensions: (x: 3, y: 3)
Dimensions without coordinates: x, y
Data variables:
a (x) int64 [s] 0 1 2
b (y) int64 [m] -3 5 1
```
to convert to different units:
```python
In [4]: c = q.pint.to({"a": "ms", "b": "km"})
...: c
Out[4]:Dimensions: (x: 3, y: 3)
Dimensions without coordinates: x, y
Data variables:
a (x) float64 [ms] 0.0 1e+03 2e+03
b (y) float64 [km] -0.003 0.005 0.001
```
to convert back to non-quantities:
```python
In [5]: d = c.pint.dequantify()
...: d
Out[5]:Dimensions: (x: 3, y: 3)
Dimensions without coordinates: x, y
Data variables:
a (x) float64 0.0 1e+03 2e+03
b (y) float64 -0.003 0.005 0.001
```For more, see the [documentation](https://pint-xarray.readthedocs.io)