将 Serie (pandas) 转换为 DataArray (xarray),保持维度值的当前顺序

Convert a Serie (pandas) to DataArray (xarray) keeping the current order of the values of the dimensions

有一种方法可以将 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

在此示例中,维度 "city" 具有以下值:

'Las Perdices' 'Córdoba' 'General Deheza'

所以在 运行 .to_xarray() (从 serie 转换为 xarray)之后,值的顺序更改为:

'Córdoba' 'General Deheza' 'Las Perdices'

有什么办法可以防止这种行为吗?

pandas 中的许多重塑操作将导致索引排序,包括 to_xarray,但也包括 unstack:

In [5]: se.unstack()
Out[5]:
year            2018  2019
city
Córdoba           30    40
General Deheza    50    60
Las Perdices      10    20

保持排序的唯一方法是为您的城市列表使用 CategoricalIndex:

In [2]: se = pd.Series(
   ...:     np.arange(10, 70, 10),
   ...:     index=pd.MultiIndex.from_product([
   ...:         pd.Categorical(
   ...:             ["Las Perdices","Córdoba","General Deheza"],
   ...:             categories=["Las Perdices","Córdoba","General Deheza"],
   ...:             ordered=True),
   ...:         [2018, 2019]],
   ...:         names=['city', 'year']))

这会明确保留排序顺序:

In [3]: se.sort_index()
Out[3]:
city            year
Las Perdices    2018    10
                2019    20
Córdoba         2018    30
                2019    40
General Deheza  2018    50
                2019    60
dtype: int64

现在您的索引顺序保存在 xarray 中:

In [4]: se.to_xarray()
Out[4]:
<xarray.DataArray (city: 3, year: 2)>
array([[10, 20],
       [30, 40],
       [50, 60]])
Coordinates:
  * city     (city) object 'Las Perdices' 'Córdoba' 'General Deheza'
  * year     (year) int64 2018 2019

Categorical data 上的 pandas 文档提供了有关创建分类序列和索引的有用提示,并提供了使用说明。

如果您希望从 xarray 进行往返,只需将 pd.Categorical() 位放在示例中创建 city 坐标的位置即可。