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

DataArray coords: tuple vs. list #1016

Closed
jonathanstrong opened this issue Sep 23, 2016 · 2 comments · Fixed by #2523
Closed

DataArray coords: tuple vs. list #1016

jonathanstrong opened this issue Sep 23, 2016 · 2 comments · Fixed by #2523

Comments

@jonathanstrong
Copy link

is there a reason the coords dictionary values cannot be a tuple? I found this fairly baffling to debug when it tripped me up.

# this works
xarray.DataArray(np.random.random((3, 3, 3)),
    dims=('one', 'two', 'three'),
    coords={
         'one': ['four', 'five', 'six'],
    }
)

# this throws the following error
# ValueError: dimensions ('four',) must have the same length as the number of data dimensions, ndim=0
xarray.DataArray(np.random.random((3, 3, 3)),
    dims=('one', 'two', 'three'),
    coords={
         'one': ('four', 'five', 'six'),
    }
)

even if there was a clearer error it would help quite a bit. As it stands you are thinking, 'what?! four isn't a dimension!'

using 0.8.2

@shoyer
Copy link
Member

shoyer commented Sep 23, 2016

Tuples are overloaded so you can avoid writing another constructor -- they're interpreted as (dims, data[, attrs]), e.g., as described in the Dataset constructor. Otherwise it gets quite verbose to specify coordinates with dimensions.

Looking at the docs more carefully, we do mention this in the general overview of data structures, but not the API docs for DataArray.__init__. We should probably fix that.

@ralexx
Copy link

ralexx commented Oct 14, 2016

This isn't clear to me either from reading the Data Structures section of the docs.

In my case I triggered the error by attempting to instantiate a DataArray passing a nested tuple coords=[('dim1', (point1, point2, point3)),...]. Since I consider tuple(list()) to be an anti-pattern I started looking elsewhere for the source of the error. Making explicit the use of tuple overloading would be helpful.

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

Successfully merging a pull request may close this issue.

4 participants