Skip to content

Commit 1ecef16

Browse files
authored
Merge pull request #7 from keewis/pint-xarray
Add `pint-xarray`
2 parents 27ac446 + 53b68c3 commit 1ecef16

File tree

4 files changed

+103
-1
lines changed

4 files changed

+103
-1
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
# xarray-indexes
2+
23
A gallery of custom Xarray indexes

docs/agnostic/pint.md

Lines changed: 98 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,98 @@
1-
# PintIndex
1+
---
2+
jupytext:
3+
text_representation:
4+
format_name: myst
5+
kernelspec:
6+
display_name: Python 3
7+
name: python
8+
---
9+
10+
# pint-xarray: PintIndex
11+
12+
````{grid}
13+
```{grid-item}
14+
:columns: 3
15+
```{image} https://pint.readthedocs.io/en/latest/_static/logo-full.jpg
16+
---
17+
alt: pint logo
18+
width: 200px
19+
align: center
20+
---
21+
```
22+
```{grid-item}
23+
:columns: 9
24+
```{seealso}
25+
Learn more at the [pint-xarray](https://pint-xarray.readthedocs.io/en/latest/) documentation page.
26+
```
27+
````
28+
29+
## Highlights
30+
31+
`pint-xarray` provides an index that wraps other indexes and attaches units to the indexed coordinates. This allows operations like `integrate` or `sel` to take the units into account.
32+
33+
## Example
34+
35+
First we open the dataset, fill in missing `units` attributes, and calculate the length of the vectors for later:
36+
37+
```{code-cell} python
38+
import numpy as np
39+
import xarray as xr
40+
41+
xr.set_options(
42+
display_expand_indexes=True, display_expand_attrs=False, display_expand_data=False
43+
)
44+
45+
ds = (
46+
xr.tutorial.open_dataset("eraint_uvz")
47+
.load()
48+
.assign_coords(month=lambda ds: ds["month"].assign_attrs({"units": "months"}))
49+
.assign(windspeed=lambda ds: np.hypot(ds["u"], ds["v"]))
50+
)
51+
ds
52+
```
53+
54+
### Quantifying
55+
56+
Now we can quantify to convert arrays with a `"units"` attribute to quantity arrays:
57+
58+
```{code-cell} python
59+
import cf_xarray.units
60+
import pint_xarray
61+
62+
quantified = ds.pint.quantify()
63+
quantified
64+
```
65+
66+
Note how all variables are associated with a {py:class}`pint.Quantity` array, and how all coordinate variables are associated with a {py:class}`pint_xarray.PintIndex` wrapping a `PandasIndex`.
67+
68+
### Selection
69+
70+
With the `PintIndex`, selecting with quantities will convert the indexers to the index' units:
71+
72+
```{code-cell} python
73+
ureg = pint_xarray.unit_registry
74+
75+
quantified.sel(
76+
latitude=slice(ureg.Quantity(4800, "arcmin"), ureg.Quantity(600, "arcmin")),
77+
longitude=slice(ureg.Quantity(-10, "degree"), ureg.Quantity(np.pi, "radians")),
78+
)
79+
```
80+
81+
or raise on incompatible units:
82+
83+
```{code-cell} python
84+
---
85+
tags: [raises-exception]
86+
---
87+
quantified.sel(month=ureg.Quantity(10, "m"))
88+
```
89+
90+
### Numerical operations
91+
92+
We can also perform numerical operations, like integration:
93+
94+
```{code-cell} python
95+
quantified["windspeed"].integrate("month")
96+
```
97+
98+
Note how the units are displayed as `"meter * months / second"` and not the expected `"meter"`? This is caused by `pint` trying avoid implicit conversions as much as possible, which can substantially reduce the amount of computations.

docs/conf.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,4 +112,6 @@
112112
"xarray": ("https://docs.xarray.dev/en/stable/", None),
113113
"rasterix": ("https://rasterix.readthedocs.io/en/latest/", None),
114114
"xvec": ("https://xvec.readthedocs.io/en/stable/", None),
115+
"pint-xarray": ("https://pint-xarray.readthedocs.io/en/latest/", None),
116+
"pint": ("https://pint.readthedocs.io/en/stable/", None),
115117
}

requirements.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,3 +17,5 @@ rasterix
1717
rioxarray
1818
xvec
1919
git+https://github.com/dcherian/rolodex
20+
pint-xarray
21+
cf_xarray

0 commit comments

Comments
 (0)