简体   繁体   English

如何对xarray数据集/数据数组的索引进行排序?

[英]How to sort the index of a xarray Dataset/DataArray?

I am creating a DataArray from multiple slices along the time dimension and stumbled across the 'index must be monotonic for resampling' error when trying to resample which I guess says that my time index is not sorted. 我正在沿时间维度从多个切片创建一个DataArray ,并在尝试'index must be monotonic for resampling'时偶然发现'index must be monotonic for resampling'错误,我猜这是我的时间索引未排序。 I did not pay attention to the order in when I concat() inated them. 当我concat()它们时,我没有注意顺序。

# TODO: sort instead of raising an error

is to be found in the code where the error is raised. 将在引发错误的代码中找到。

My question: How would I sort the indices of my DataArray s within a DataSet ? 我的问题:如何排序DataSet DataArray的索引? I could not find anything like sort_index() . 我找不到诸如sort_index()类的东西。

You could reindex on the sorted index - .reindex(time=sorted(dataset.time)) 您可以在排序后的索引上重新索引.reindex(time=sorted(dataset.time))

Not as elegant as sort_index() but it should work 不如sort_index()优雅,但它应该可以工作

EDIT: @scottclowe points out that xarray since added .sortby() , so use that! 编辑:@scottclowe指出自从添加了.sortby()以来的.sortby() ,所以使用它! http://xarray.pydata.org/en/stable/generated/xarray.DataArray.sortby.html http://xarray.pydata.org/en/stable/generated/xarray.DataArray.sortby.html

现在有可以使用的sortby()方法。

da = da.sortby('time')

I had to add .values to get sorted to work correctly. 我必须添加.values才能进行排序以正常工作。 Where my time.values is a datetime64[ns]. 我的time.values是datetime64 [ns]。 Xarray v0.10.0. Xarray v0.10.0。

.reindex(time=sorted(dataset.time.values))

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

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