如何对 xarray Dataset/DataArray 的索引进行排序?

Tom*_*101 2 python pandas python-xarray

我正在DataArray沿着时间维度从多个切片创建一个,并'index must be monotonic for resampling'在尝试重新采样时偶然发现了错误,我猜我的时间索引没有排序。我没有注意它们的顺序concat()

# TODO: sort instead of raising an error

将在引发错误的代码中找到。

我的问题:我将如何DataArray在 a 中对我的s的索引进行排序DataSet?我找不到类似的东西sort_index()

sco*_*owe 6

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

da = da.sortby('time')
Run Code Online (Sandbox Code Playgroud)


Max*_*ian 5

您可以重新索引已排序的索引 - .reindex(time=sorted(dataset.time))

没有那么优雅,sort_index()但它应该可以工作

编辑:@scottclowe 指出 xarray 自添加以来.sortby(),所以使用它!http://xarray.pydata.org/en/stable/generated/xarray.DataArray.sortby.html