unstack() treats string coords as objects · Issue #907 · pydata/xarray (original) (raw)

unstack() should be smart enough to recognise that all labels in a coord are strings, and convert them to numpy strings.
This is particularly relevant e.g. if you want to dump the xarray to netcdf and then read it with a non-python library.

import xarray

a = xarray.DataArray([[1,2],[3,4]], dims=['x', 'y'], coords={'x': ['x1', 'x2'], 'y': ['y1', 'y2']}) a

<xarray.DataArray (x: 2, y: 2)>
array([[1, 2],
       [3, 4]])
Coordinates:
  * y        (y) <U2 'y1' 'y2'
  * x        (x) <U2 'x1' 'x2'

a.stack(s=['x', 'y']).unstack('s')

<xarray.DataArray (x: 2, y: 2)>
array([[1, 2],
       [3, 4]])
Coordinates:
  * x        (x) object 'x1' 'x2'
  * y        (y) object 'y1' 'y2'