【发布时间】:2018-10-31 14:19:33
【问题描述】:
有一种方法可以将 Serie (pandas) 转换为 DataArray (xarray) 并保持维度值的当前顺序?
当有多个维度时会出现问题。 例如:
In [1]: import xarray as xr
In [2]: coord1 = ("city",["Las Perdices","Córdoba","General Deheza"])
: coord2 = ("year",[2018,2019])
In [3]: da = xr.DataArray([[10,20],[30,40],[50,60]],coords=[coord1,coord2])
: da
Out[3]:
<xarray.DataArray (city: 3, year: 2)>
array([[10, 20],
[30, 40],
[50, 60]])
Coordinates:
* city (city) <U14 'Las Perdices' 'Córdoba' 'General Deheza'
* year (year) int32 2018 2019
In [4]: se = da.to_series()
: se
Out[4]:
city year
Las Perdices 2018 10
2019 20
Córdoba 2018 30
2019 40
General Deheza 2018 50
2019 60
dtype: int32
In [5]: newArr = se.to_xarray()
: newArr
Out[5]:
<xarray.DataArray (city: 3, year: 2)>
array([[30, 40],
[50, 60],
[10, 20]])
Coordinates:
* city (city) object 'Córdoba' 'General Deheza' 'Las Perdices'
* year (year) int64 2018 2019
在此示例中,维度“城市”具有以下值:
'Las Perdices' 'Córdoba' 'General Deheza'
所以在运行 .to_xarray() 后(从 serie 转换为 xarray),值的顺序变为:
'Córdoba' 'General Deheza' 'Las Perdices'
有没有办法防止这种行为?
【问题讨论】:
标签: python-xarray