简体   繁体   English

Python Xarray,按索引或维度排序?

[英]Python Xarray, sort by index or dimension?

Is there a sort_index or sort_by_dimension method of some kind in xarray , much like pandas.DataFrame.sort_index() , where I can sort a xarray.DataArray object by one of its dimensions? xarray 中是否有某种sort_indexsort_by_dimension方法,就像xarray pandas.DataFrame.sort_index()一样,我可以在其中对xarray.DataArray ZA8CFDE6331BD59EB2AC96F891 中的一个维度进行排序? In terms of usage, I'm thinking of something like data_array.sort(dim="dimension_name") .在使用方面,我正在考虑类似data_array.sort(dim="dimension_name")的东西。

I couldn't find a good method to do this built into xarray , so I made a new array by taking a slice with the sorted values from the coordinate I wanted to sort: da_sorted=da.loc[{'lon':sorted(da.coords['lon'].values)}] 我找不到一个很好的方法来构建xarray ,所以我通过从我想要排序的坐标中取出一个切片值来创建一个新数组: da_sorted=da.loc[{'lon':sorted(da.coords['lon'].values)}]

Here's a larger example with test data showing what it looks like in practice: 这是一个更大的例子,测试数据显示了它在实践中的样子:

import numpy as np
import xarray as xr

data = np.random.rand(2, 3)

lat = [47, 45]

lon = [-118, -122, -120]
da = xr.DataArray(data, coords=[lat, lon], dims=['lat', 'lon'])


>>>> da
<xarray.DataArray (lat: 2, lon: 3)>
array([[ 0.16118 ,  0.215621,  0.599749],
       [ 0.144778,  0.984167,  0.416469]])
Coordinates:
  * lat      (lat) int64 47 45
  * lon      (lon) int64 -118 -122 -120,


da_sorted = da.loc[{'lon':sorted(da.coords['lon'].values)}]
>>>> da_sorted
<xarray.DataArray (lat: 2, lon: 3)>
array([[ 0.215621,  0.599749,  0.16118 ],
       [ 0.984167,  0.416469,  0.144778]])
Coordinates:
  * lat      (lat) int64 47 45
  * lon      (lon) int64 -122 -120 -118

Do you mean rearrange the dimensions? 你是说重新排列尺寸? For example, with .transpose() you can reorder the dimensions of both a whole dataset or specific dataarray, eg 例如,使用.transpose()您可以重新排序整个数据集或特定数据阵列的维度,例如

ds.transpose('lat','lon','time')

xarray has now a built-in method for this task, which can be performed easily using .sortby() https://xarray.pydata.org/en/stable/generated/xarray.DataArray.sortby.html xarray现在有一个用于此任务的内置方法,可以使用.sortby() https://xarray.pydata.org/en/stable/generated/xarray.DataArray.sortby.html轻松执行

The code posted by @Kyle Heuton becomes now: @Kyle Heuton发布的代码现在变为:

import numpy as np
import xarray as xr

data = np.random.rand(2, 3)

lat = [47, 45]

lon = [-118, -122, -120]
da = xr.DataArray(data, coords=[lat, lon], dims=['lat', 'lon'])


>>>> da
<xarray.DataArray (lat: 2, lon: 3)>
array([[ 0.16118 ,  0.215621,  0.599749],
       [ 0.144778,  0.984167,  0.416469]])
Coordinates:
  * lat      (lat) int64 47 45
  * lon      (lon) int64 -118 -122 -120,


da_sorted = da.sortby(da.lon)
>>>> da_sorted
<xarray.DataArray (lat: 2, lon: 3)>
array([[ 0.215621,  0.599749,  0.16118 ],
       [ 0.984167,  0.416469,  0.144778]])
Coordinates:
  * lat      (lat) int64 47 45
  * lon      (lon) int64 -122 -120 -118

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM