解释关节运动的xarray和numpy之间的尺寸不匹配行为

时间:2019-05-06 14:11:21

标签: python python-xarray

对于一个气象项目,我将xarray DataArrays和Numpy Arrays的功能结合在一起。对于大多数用例来说,这很好,但是对于一个用例来说,事情就出错了。当我想将仅包含一个变量的xarray DataArray与Numpy数组向量相乘时,会发生这种情况。

foo = np.array([100, 200, 300]) # Create Numpy vector
bar = np.array(3)  # Create Numpy array with only one number

bar = xr.DataArray(bar)  # Convert Numpy array to Xarray DataArray

foo*bar  # Multiply the Numpy array with the 0-D Xarray DataArray. This results 
# in an error

我希望乘法会导致以xarray DataArray作为结果的[300, 600, 900],但是会引发ValueError。我的问题是:为什么这种乘法会导致错误?

这是回溯:

  File "C:\Anaconda3\lib\site-packages\spyder\utils\site\sitecustomize.py", line 101, in execfile
    exec(compile(f.read(), filename, 'exec'), namespace)

  File "D:\test\test.py", line 16, in <module>
    bar*foo

  File "C:\Anaconda3\lib\site-packages\xarray\core\dataarray.py", line 1972, in func
    if not reflexive

  File "C:\Anaconda3\lib\site-packages\xarray\core\variable.py", line 1676, in func
    result = Variable(dims, new_data, attrs=attrs)

  File "C:\Anaconda3\lib\site-packages\xarray\core\variable.py", line 260, in __init__
    self._dims = self._parse_dimensions(dims)

  File "C:\Anaconda3\lib\site-packages\xarray\core\variable.py", line 424, in _parse_dimensions
    % (dims, self.ndim))

ValueError: dimensions () must have the same length as the number of data dimensions, ndim=1```

0 个答案:

没有答案