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

error when quantifying a dimension #105

Closed
keewis opened this issue May 28, 2021 · 0 comments · Fixed by #174
Closed

error when quantifying a dimension #105

keewis opened this issue May 28, 2021 · 0 comments · Fixed by #174

Comments

@keewis
Copy link
Collaborator

keewis commented May 28, 2021

Trying to quantify a DataArray describing a dimension after pulling it out fails:

In [2]: ds = xr.Dataset(coords={"x": ("x", [0, 1], {"units": "m"}), "y": ("y", [5, 6, 7], {"units": "m"})})
   ...: ds
Out[2]: 
<xarray.Dataset>
Dimensions:  (x: 2, y: 3)
Coordinates:
  * x        (x) int64 0 1
  * y        (y) int64 5 6 7
Data variables:
    *empty*

In [3]: q = ds.pint.quantify()
   ...: q
Out[3]: 
<xarray.Dataset>
Dimensions:  (x: 2, y: 3)
Coordinates:
  * x        (x) int64 0 1
  * y        (y) int64 5 6 7
Data variables:
    *empty*

In [4]: q.x
Out[4]: 
<xarray.DataArray 'x' (x: 2)>
array([0, 1])
Coordinates:
  * x        (x) int64 0 1
Attributes:
    units:    meter

In [5]: q.x.pint.quantify()
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-5-ac870069d90f> in <module>
----> 1 q.x.pint.quantify()

.../pint_xarray/accessors.py in quantify(self, units, unit_registry, **unit_kwargs)
    317         units = either_dict_or_kwargs(units, unit_kwargs, "quantify")
    318 
--> 319         registry = get_registry(unit_registry, units, conversion.extract_units(self.da))
    320 
    321         unit_attrs = conversion.extract_unit_attributes(self.da)

.../pint_xarray/conversion.py in extract_units(obj)
    248         name = obj.name if obj.name is not None else "<this-array>"
    249 
--> 250         ds = obj.rename(name).to_dataset()
    251 
    252         units = extract_units(ds)

.../xarray/core/dataarray.py in to_dataset(self, dim, name, promote_attrs)
    576             result = self._to_dataset_split(dim)
    577         else:
--> 578             result = self._to_dataset_whole(name)
    579 
    580         if promote_attrs:

.../xarray/core/dataarray.py in _to_dataset_whole(self, name, shallow_copy)
    525             )
    526         if name in self.coords:
--> 527             raise ValueError(
    528                 "cannot create a Dataset from a DataArray with "
    529                 "the same name as one of its coordinates"

ValueError: cannot create a Dataset from a DataArray with the same name as one of its coordinates

We should try to fix this.

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.

1 participant