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

Coordinates not quantified / quantifiable #124

Closed
mueslo opened this issue Jul 18, 2021 · 4 comments · Fixed by #126
Closed

Coordinates not quantified / quantifiable #124

mueslo opened this issue Jul 18, 2021 · 4 comments · Fixed by #126

Comments

@mueslo
Copy link

mueslo commented Jul 18, 2021

If I have a DataArray or Dataset, I would expect there to be some way to quantify not just the data, but also the coordinates. <o>.pint.quantify() seems to only quantify the data.

import xarray as xr
import pint
import pint_xarray
ureg = pint.UnitRegistry(force_ndarray_like=True)

ds = xr.Dataset(
    {'z': (("x","y"), np.random.random((2, 3)), {"units": "m"})},
    coords={"x": ("x", [0, 1], {"units": "m"}), 
            "y": ("y", [5, 6, 7], {"units": "m"})})
ds = ds.pint.quantify(unit_registry=ureg)
ds.x + 2*ureg.cm
# DimensionalityError: Cannot convert from 'dimensionless' to 'centimeter'

Manually running quantify on the coordinate gives:

ds.x.pint.quantify()
# ValueError: cannot create a Dataset from a DataArray with the same name as one of its coordinates

Manually constructing from quantified coordinates leads to an explicit dequantify:

x = xr.DataArray([0, 1], attrs={'units': 'm'}, dims=('x',))
x = x.pint.quantify(unit_registry=ureg)

ds = xr.Dataset(
    {'z': (("x","y"), np.random.random((2, 3)), {"units": "m"})},
    coords={"x": x, 
            "y": ("y", [5, 6, 7], {"units": "m"})})
# UnitStrippedWarning: The unit of the quantity is stripped when downcasting to ndarray.
#ds = ds.pint.quantify(unit_registry=ureg)
ds.x + 2*ureg.cm
# DimensionalityError: Cannot convert from 'dimensionless' to 'centimeter'

Is this functionality not expected to work?

@mueslo
Copy link
Author

mueslo commented Jul 18, 2021

Okay, what I was doing was more like

ds = xr.Dataset(
    {'z': (("x","y"), np.random.random((2, 3)), {"units": "m"})},
    coords={"x": [0, 1], 
            "y": [5, 6, 7]})
ds.x.attrs['units'] = 'm' 
ds = ds.pint.quantify(unit_registry=ureg)

which leads to ds.z being correctly quantified, but not ds.x

Instead running

ds = xr.Dataset(
    {'z': (("x","y"), np.random.random((2, 3)), {"units": "m"})},
    coords={"xx": ("x", [0, 1], {"units": "m"}), 
            "yy": ("y", [5, 6, 7], {"units": "m"})})
ds = ds.pint.quantify(unit_registry=ureg)

Leads to both being correctly quantified. As a beginner with pint/xarray, it's not obvious.

Edit: Okay, but this leads to a Dataset/DataArray without associated indexed dimension coordinates. It seems the index coordinate always drops units. Why?

@mueslo
Copy link
Author

mueslo commented Jul 18, 2021

https://pint-xarray.readthedocs.io/en/latest/generated/xarray.Dataset.pint.quantify.html#xarray-dataset-pint-quantify

As units in dimension coordinates are not supported until xarray changes the way it implements indexes, these units will be set as attributes.

@mueslo mueslo closed this as completed Jul 18, 2021
@keewis
Copy link
Collaborator

keewis commented Jul 18, 2021

Note that we're currently actively working on removing that restriction in xarray (will still need some time, though). Once that is done this should just work.

In the meantime, I guess we could make the note more obvious, maybe by not combining it with the one for dask or by moving the dask note to the Notes section and making this one a warning.

@keewis
Copy link
Collaborator

keewis commented Jul 23, 2021

reopening, to be closed by #126

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants