-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Problem with creating coords by dict #1197
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
Comments
You shouldn't be able to make a DataArray like this -- you have three dimensions on coordinates, but only one dimension on the array itself. With xarray 0.8.2, you get an error message with your example:
However, this error message is not quite accurate. You need to explicitly provide
I still need to test this on the dev version, but for now I will make this as an error reporting bug. |
Hello, thanks for the immediate reply - and to seize the opportunity: thanks for this really great library, which I think will be the enabler for me to - finally - use netCDF4 for my measurement facility like I wished for years now. Besides, the working alternatives with tuples or the OrderedDict use the very same dummy array for initializing in the example above. Further I estimate the 0.8.2-error-message completely consistent: it (silently) acknowledges that there are three dimensions in both the zeros-array and the coords by reporting a problem about not fitting lengths of the third one of each, 'z'. Though by adding a one-dimensional dims-definition, you turn the following error messages away from the original problem, now indeed introducing an inconsistency in dimensions between dims and data. |
Note that the recommended to build data arrays is to specify the dimensions names explicitly:
on master, failing to do so results in a
|
@shoyer maybe changing the warning into an error would be the way to go now? |
@SpghttCd Yes, I was confused. I missed that you were writing On master (and in the next release of xarray), we do indeed issue a warning here as @fmaussion points out. Unfortunately, we did support this behavior in prior releases of xarray (especially with an |
@shoyer - Do we still want to deprecate this behavior in v0.10? |
Sure, that seems reasonable to me. |
IMO there's a problem with using dicts as coords definition for DataArrays.
E.g. an array defined by
import xarray as xr
arr = xr.DataArray(np.zeros([1, 2, 3]), {'x': [1], 'y': [1, 2], 'z': [1, 2, 3]})
will lead to an exception when tried to be indexed like
arr[0, 0, 0]
The error message is
ValueError: conflicting sizes for dimension 'z': length 2 on <this-array> and length 3 on 'z'
As far as I can imagine this is because dicts don't preserve order in python.
At least printing the coords shows a different order than used in the definition of
arr
:arr.coords
Coordinates:
* x (x) int32 1
* z (z) int32 1 2 3
* y (y) int32 1 2
Using an ordered dict turns out to work:
import collections
od = collections.OrderedDict([('x', [1]), ('y', [1, 2]), ('z', [1, 2, 3])])
arr = xr.DataArray(np.zeros([1, 2, 3]), od)
And so does using a list of tuples:
arr = xr.DataArray(np.zeros([1, 2, 3]), [('x', [1]), ('y', [1, 2]), ('z', [1, 2, 3])])
Both lead to
arr[0, 0, 0]
<xarray.DataArray ()>
array(0.0)
Coordinates:
x int32 1
y int32 1
z int32 1
as expected.
Detected in WinPython-64bit-3.4.4.1Qt5 with xarray version 0.7.0
The text was updated successfully, but these errors were encountered: