【问题标题】:Convert a Serie (pandas) to DataArray (xarray) keeping the current order of the values of the dimensions将 Serie (pandas) 转换为 DataArray (xarray),保持维度值的当前顺序
【发布时间】: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


    【解决方案1】:

    pandas 中很多 reshape 操作都会导致索引被排序,包括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 坐标的位置。

    【讨论】:

    猜你喜欢
    • 2021-03-12
    • 1970-01-01
    • 2017-01-30
    • 2019-06-28
    • 2021-10-29
    • 1970-01-01
    • 2019-04-02
    • 2020-11-14
    • 2022-01-06
    相关资源
    最近更新 更多