unstack() sorts data alphabetically · Issue #906 · pydata/xarray (original) (raw)
DataArray.unstack() sorts the data alphabetically by label.
Besides being poor for performance, this is very problematic whenever the order matters, and the labels are not in alphabetical order to begin with.
import xarray import pandas
index = [ ['x1', 'first' ], ['x1', 'second'], ['x1', 'third' ], ['x1', 'fourth'], ['x0', 'first' ], ['x0', 'second'], ['x0', 'third' ], ['x0', 'fourth'], ] index = pandas.MultiIndex.from_tuples(index, names=['x', 'count']) s = pandas.Series(list(range(8)), index) a = xarray.DataArray(s) a
<xarray.DataArray (dim_0: 8)>
array([0, 1, 2, 3, 4, 5, 6, 7], dtype=int64)
Coordinates:
* dim_0 (dim_0) object ('x1', 'first') ('x1', 'second') ('x1', 'third') ...
<xarray.DataArray (x: 2, count: 4)>
array([[4, 7, 5, 6],
[0, 3, 1, 2]], dtype=int64)
Coordinates:
* x (x) object 'x0' 'x1'
* count (count) object 'first' 'fourth' 'second' 'third'