pythonpython-xarray

how do I remove unwanted empty dimension from xarray DataArray (squeeze doesn't work)


I have a DataArray that has four dimension, as you can see time has a size of 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] 

I need my DataArray to be in the format ('Y','X','color') however when I try to get rid of the time dimension using squeeze:

new_darray = old_darray.squeeze('time')

this happens:

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

and when I try this:

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

this happens too:

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

Solution

  • You can't index or squeeze out a size 0 dimension, in either xarray or NumPy. That would entail transforming an empty array of size 3 * 0 * 279 * 496 = 0 into an array full of data of size 3 * 279 * 496 = 415152. Where would the new data come from?