如何从xarray DataArray中删除不需要的空维度(挤压不起作用)

时间:2017-05-03 06:59:21

标签: python python-xarray

我有一个具有四维的DataArray,因为你可以看到时间大小为0:

<xarray.DataArray (color: 3, time: 0, y: 297, x: 496)>
array([], shape=(3, 0, 297, 496), dtype=float64)
Coordinates:
  * y        (y) float64 -3.199e+06 -3.199e+06 -3.199e+06 -3.199e+06 ...
  * x        (x) float64 1.966e+06 1.966e+06 1.966e+06 1.966e+06 1.966e+06 ...
  * color    (color) 'red' 'green' 'blue'
  * time     (time) datetime64[ns] 

我需要我的DataArray采用格式('Y','X','color')但是当我尝试使用squeeze去掉时间维度时:

new_darray = old_darray.squeeze('time')

这种情况发生了:

IndexError: index 0 is out of bounds for axis 0 with size 0

当我尝试这个时:

new_darray = old_darray[:,0,:,:]

这也发生了:

IndexError: index 0 is out of bounds for axis 0 with size 0 

1 个答案:

答案 0 :(得分:1)

您无法在xarray或NumPy中索引或挤出大小为0的维度。这将需要将大小为3 * 0 * 279 * 496 = 0的空数组转换为一个大小为3 * 279 * 496 = 415152的数组。新数据来自哪里?