|
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. |
0 commit comments