Skip to content

Confusing error (or lack thereof) when coordinate and variable share the same name #631

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

Closed
anntzer opened this issue Oct 19, 2015 · 6 comments

Comments

@anntzer
Copy link
Contributor

anntzer commented Oct 19, 2015

It probably makes sense to prevent dataset to have variables sharing the names of coordinates (what would dataset.varname return?) but currently

Dataset({"a": DataArray(np.zeros((3, 4)), dims=["a", "b"],
                        coords={"a": list("xyz"), "b": list("xyzt")})})

fails with ValueError: an index variable must be defined with 1-dimensional data, and

Dataset({"a": DataArray(np.zeros(3), coords={"a": list("xyz")})})

actually creates an empty dataset using [0, 0, 0] as values for the a coordinate instead of x y z:

<xray.Dataset>
Dimensions:  (a: 3)
Coordinates:
  * a        (a) float64 0.0 0.0 0.0
Data variables:
    *empty*
@shoyer shoyer added the bug label Oct 20, 2015
@shoyer
Copy link
Member

shoyer commented Oct 20, 2015

Oh my, this certainly does look like a bug (or several).

@markelg
Copy link
Contributor

markelg commented Oct 20, 2015

This is related to #457. In the first case, I think that the error message must be improved, but I do not think that Error is itself is a bug. From the netCDF data model point of view, it does not make sense to define a two dimensional variable sharing the name with a one-dimensional coordinate.

The second example does look like a bug, as it should raise an error too, in my opinion, as the function Dataset is given contradictory information (two different sets of values for the same variable "a"). In netCDF files you cannot have two variables with the same name, and coordinate variables are just regular variables.

@shoyer
Copy link
Member

shoyer commented Oct 20, 2015

Yes, @markelg I think you describe each of these issues correctly. They should each result in more informative errors.

@jhamman
Copy link
Member

jhamman commented Sep 7, 2017

The first example here was fixed in #1539.

The second issue still exists but now requires dims to be specified explicitly.

In [9]: Dataset({"a": DataArray(np.zeros(3), dims='a', coords={"a": list("xyz")})})
   ...: 
   ...: 
Out[9]: 
<xarray.Dataset>
Dimensions:  (a: 3)
Coordinates:
  * a        (a) float64 0.0 0.0 0.0
Data variables:
    *empty*

Do we agree it is safe to say that no key should be repeated between data and coords?

@max-sixty
Copy link
Collaborator

Adding a v confusing error message which my colleague just hit (not exactly the same as this, but thought it wasn't worth a whole new issue):


In [10]: xr.DataArray(
    ...:     data=1,
    ...:     coords={
    ...:         'test':[1]#,
    ...:     },
    ...:     dims=['test']
    ...: )
---------------------------------------------------------------------------
KeyError                                  Traceback (most recent call last)
<ipython-input-10-41a019d49aae> in <module>
      4         'test':[1]#,
      5     },
----> 6     dims=['test']
      7 )

~/workspace/xarray/xarray/core/dataarray.py in __init__(self, data, coords, dims, name, attrs, encoding, indexes, fastpath)
    228
    229             data = as_compatible_data(data)
--> 230             coords, dims = _infer_coords_and_dims(data.shape, coords, dims)
    231             variable = Variable(dims, data, attrs, encoding, fastpath=True)
    232

~/workspace/xarray/xarray/core/dataarray.py in _infer_coords_and_dims(shape, coords, dims)
     76
     77         for d, s in zip(v.dims, v.shape):
---> 78             if s != sizes[d]:
     79                 raise ValueError('conflicting sizes for dimension %r: '
     80                                  'length %s on the data but length %s on '

KeyError: 'test'

...nothing to do with the test entry, it's that data is a scalar

@kmuehlbauer
Copy link
Contributor

All given examples work in recent xarray 2024.10.0. Please reopen with updated examples, if this is still an issue.

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

No branches or pull requests

6 participants