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

Multiple methods fail on a DataArray with scalar dimension #1709

Closed
suyashbire1 opened this issue Nov 12, 2017 · 2 comments
Closed

Multiple methods fail on a DataArray with scalar dimension #1709

suyashbire1 opened this issue Nov 12, 2017 · 2 comments

Comments

@suyashbire1
Copy link

Code Sample, a copy-pastable example if possible

import numpy as np
import xarray as xr
a = np.arange(20).reshape(1,2,2,5)
coords = dict(t=0, z=np.arange(2), y=np.arange(2), x=np.arange(5))
b = xr.DataArray(a,dims=['t', 'z', 'y', 'x'],coords=coords)
b.squeeze() # Throws ValueError: dimension 't' already exists as a scalar variable

coords = dict(t=[0], z=np.arange(2), y=np.arange(2), x=np.arange(5))
c = xr.DataArray(a,dims=['t', 'z', 'y', 'x'],coords=coords)
c.squeeze()  # This works!

Problem description

Besides squeeze, a number of other methods like drop fail on DataArray b. Since, scalar dimensions are not dropped, it is almost impossible to do anything useful like plot, concat, etc with b. All these methods fail with ValueError: dimension 't' already exists as a scalar variable.

Expected Output

In my opinion, b and c should behave in the same way. It would just involve converting the scalar dimensions into lists. Is this behavior a bug or by design?

Output of xr.show_versions()

0.9.6

@shoyer
Copy link
Member

shoyer commented Nov 12, 2017

Thanks for the report.

This should actually raise an error at construction time for the DataArray. Variables with names matching dimensions are special in xarray: they are required to be 1D arrays with length matching the dimension size.

@suyashbire1
Copy link
Author

Glad I could help.

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

2 participants