Skip to content

Commit 20a9301

Browse files
keewisbenbovy
andauthored
implement the PintIndex (#163)
* add a `PintMetaIndex` that for now can only `sel` * add a function to compare indexers * expect indexer dicts for strip_indexer_units * move the indexer comparison function to the utils * change extract_indexer_units to expect a dict * fix a few calls to extract_indexer_units * one more call * implement `create_variables` and `from_variables` Co-authored-by: Benoit Bovy <[email protected]> * use the new index to attach units to dimension coordinates * pass the dictionary of indexers instead iterating manually * use `Coordinates._construct_direct` * delegate `isel` to the wrapped index and wrap the result * add a inline `repr` for the index * stubs for the remaining methods * rename the index class to `PintIndex` * add a utility method to wrap the output of the wrapped index's methods * implement `equals` * implement `roll`, `rename`, and `__getitem__` by forwarding * start adding tests * add tests for `create_variables` * add tests for `sel` * add tests for `isel` * improve the tests for `sel` * add tests for `equals` * add tests for `roll` * add tests for `rename` * add tests for `__getitem__` * add tests for `_repr_inline_` * configure coverage, just in case * use `_replace` instead of manually constructing the new index * explicitly check that the pint index gets created * also verify that non-quantity variables don't become `PintIndex`ed * don't use `.pint.sel` * fix `PintIndex.from_variables` and properly test it * quantify the test data This allows us to test with quantified indexes. * explicity quantify the input of the `interp_like` tests * also strip the units of `other` * change expectations in the conversion tests * refactor `attach_units_dataset` * get `convert_units` to accept indexes * strip indexes as well * change the `.pint.to` tests to not include indexes These are already covered by the conversion tests, so no need to repeat. * extract the units of `other` in `.pint.interp_like` * quantify the input and expected data in the `reindex` tests * remove the left-over explicit quantification in the `interp` tests * get `.pint.reindex` to work by explicitly converting, stripping, and then reattaching * quantify the input and expected objects in the `reindex_like` tests * get `reindex_like` to work with indexes * quantify expected only if we expect to make use of it * quantify input and expected objects in the `sel` and `loc` tests * get `.pint.sel` and `.pint.loc` to work with the indexes In theory, we could also use `sel` and `loc` directly, but that would not allow us to change the units of the result (or at least, as far as I can tell). * remove the warning about indexed coordinates * preserve the order of the variables * remove the remaining uses of `Coordinates._construct_direct` * whats-new entry * expose the index --------- Co-authored-by: Benoit Bovy <[email protected]>
1 parent ca4e282 commit 20a9301

11 files changed

+1000
-562
lines changed

docs/examples/plotting.ipynb

+1-1
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@
108108
"metadata": {},
109109
"outputs": [],
110110
"source": [
111-
"monthly_means.pint.sel(\n",
111+
"monthly_means.sel(\n",
112112
" lat=ureg.Quantity(4350, \"angular_minute\"),\n",
113113
" lon=ureg.Quantity(12000, \"angular_minute\"),\n",
114114
")"

docs/whats-new.rst

+2
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ What's new
66
------------------
77
- drop support for python 3.9 (:pull:`266`)
88
By `Justus Magin <https://github.com/keewis>`_.
9+
- create a `PintIndex` to allow units on indexed coordinates (:pull:`163`, :issue:`162`)
10+
By `Justus Magin <https://github.com/keewis>`_ and `Benoit Bovy <https://github.com/benbovy>`_.
911

1012
0.4 (23 Jun 2024)
1113
-----------------

pint_xarray/__init__.py

+2
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
from . import accessors, formatting, testing # noqa: F401
66
from .accessors import default_registry as unit_registry
77
from .accessors import setup_registry
8+
from .index import PintIndex
89

910
try:
1011
__version__ = version("pint-xarray")
@@ -21,4 +22,5 @@
2122
"testing",
2223
"unit_registry",
2324
"setup_registry",
25+
"PintIndex",
2426
]

0 commit comments

Comments
 (0)