-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Description
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