Skip to content

automatic set up of pint_xarray accessors #17

Description

@mikapfl

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions