Skip to content

Commit

Permalink
implement the PintIndex (#163)
Browse files Browse the repository at this point in the history
* 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]>
  • Loading branch information
keewis and benbovy authored Jul 9, 2024
1 parent ca4e282 commit 20a9301
Show file tree
Hide file tree
Showing 11 changed files with 1,000 additions and 562 deletions.
2 changes: 1 addition & 1 deletion docs/examples/plotting.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@
"metadata": {},
"outputs": [],
"source": [
"monthly_means.pint.sel(\n",
"monthly_means.sel(\n",
" lat=ureg.Quantity(4350, \"angular_minute\"),\n",
" lon=ureg.Quantity(12000, \"angular_minute\"),\n",
")"
Expand Down
2 changes: 2 additions & 0 deletions docs/whats-new.rst
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ What's new
------------------
- drop support for python 3.9 (:pull:`266`)
By `Justus Magin <https://github.com/keewis>`_.
- create a `PintIndex` to allow units on indexed coordinates (:pull:`163`, :issue:`162`)
By `Justus Magin <https://github.com/keewis>`_ and `Benoit Bovy <https://github.com/benbovy>`_.

0.4 (23 Jun 2024)
-----------------
Expand Down
2 changes: 2 additions & 0 deletions pint_xarray/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
from . import accessors, formatting, testing # noqa: F401
from .accessors import default_registry as unit_registry
from .accessors import setup_registry
from .index import PintIndex

try:
__version__ = version("pint-xarray")
Expand All @@ -21,4 +22,5 @@
"testing",
"unit_registry",
"setup_registry",
"PintIndex",
]
Loading

0 comments on commit 20a9301

Please sign in to comment.