Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

automatic set up of pint_xarray accessors #17

Open
mikapfl opened this issue Jan 5, 2021 · 1 comment
Open

automatic set up of pint_xarray accessors #17

mikapfl opened this issue Jan 5, 2021 · 1 comment
Labels
enhancement New feature or request

Comments

@mikapfl
Copy link
Member

mikapfl commented Jan 5, 2021

Is your feature request related to a problem? Please describe.

It's annoying that I always have to call pint_xarray.accessors.setup_registry(unit_registry) if I want to use openscm-units together with pint_xarray. Of course, this has to be done only once after importing both, like this:

import xarray as xr
import pint
import pint_xarray
from openscm_units import unit_registry

pint_xarray.accessors.setup_registry(unit_registry)

but still, it is a mouthful and not completely obvious.

Describe the solution you'd like

Ideally, openscm-units would somehow detect if used together with pint_xarray and call the accessor setup automatically.
Looking into setup_registry, it enables force_ndarray_like, so that operations with the unit_registry always yield arrays, like this:

In [1]: import xarray as xr
   ...: import pint
   ...: import pint_xarray
   ...: from openscm_units import unit_registry

In [2]: 2 * unit_registry('kg CO2')
Out[2]: 2 <Unit('CO2 * kilogram')>

In [3]: pint_xarray.accessors.setup_registry(unit_registry)
Out[3]: <openscm_units.unit_registry.ScmUnitRegistry at 0x7f6f1f448eb0>

In [4]: 2 * unit_registry('kg CO2')
Out[4]: array(2) <Unit('CO2 * kilogram')>

As a default behaviour, this would probably be annoying, and admittedly, it would be weird to change behaviour just because pint_xarray is available at import time. So maybe we need something like a unit_registry_xarray which is the same as the unit_registry, just with force_ndarray_like=True. Or maybe pint_xarray is too much of a moving target at the moment to integrate so tightly?

Describe alternatives you've considered

Alternatively, the need to call setup_registry could be documented somewhere. I could provide patches for that.

@znicholls
Copy link
Contributor

So maybe we need something like a unit_registry_xarray which is the same as the unit_registry, just with force_ndarray_like=True.

This seems like a good solution (from my very vague understanding of pint and xarray's implementation). Happy to look at a PR.

Or maybe pint_xarray is too much of a moving target at the moment to integrate so tightly?

Given how lightweight this repo is, I'm happy to play around with getting the xarray integration right. It would be interesting to see (and seems a much more promising route forward than our pandas integration which is currently stuck e.g. pandas-dev/pandas#35127).

Alternatively, the need to call setup_registry could be documented somewhere.

We should definitely do this (and/or add a reference to the pint xarray docs so people know where to look for the latest information).

@znicholls znicholls added the enhancement New feature or request label Feb 8, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants