如何更改 xarray.Dataset 维度的顺序?

Hap*_*ier 7 dataset dimensions python-xarray

我正在创建一个 xarray 数据集,如下所示:

import numpy as np
import xarray as xr

x_example = np.random.rand(1488,)
y_example = np.random.rand(1331,)
time_example = np.random.rand(120,)
rainfall_example = np.random.rand(120, 1331, 1488)

rainfall_dataset = xr.Dataset(
    data_vars=dict(
        rainfall_depth=(['time', 'y', 'x'], rainfall_example),
    ),
    coords=dict(
        time=(['time'], time_example),
        x=(['x'], x_example),
        y=(['y'], y_example)
    )
)
Run Code Online (Sandbox Code Playgroud)

结果是这样的

在此输入图像描述

而我跑步时的尺寸rainfall_example.dims是这样的Frozen({'time': 120, 'y': 1331, 'x': 1488})(这也可以从上面的结果中看出)。我知道xarray.Dataset.dims不能根据这里修改

我的问题是:我们如何才能将这些维度的顺序更改为这样的维度Frozen({'time': 120, 'x': 1488, 'y': 1331})而不更改其他任何内容(一切都将相同,只是维度的顺序发生了变化)?

Mic*_*ado 7

您可以通过使用列表按顺序选择坐标和变量来重新排序它们:

In [3]: rainfall_dataset[["time", "y", "x", "rainfall_depth"]]
Out[3]:
<xarray.Dataset>
Dimensions:         (time: 120, y: 1331, x: 1488)
Coordinates:
  * time            (time) float64 0.2848 0.7556 0.9501 ... 0.694 0.734 0.198
  * y               (y) float64 0.1941 0.1132 0.2504 ... 0.1501 0.5085 0.006135
  * x               (x) float64 0.2776 0.4504 0.1886 ... 0.4071 0.3327 0.5555
Data variables:
    rainfall_depth  (time, y, x) float64 ...
Run Code Online (Sandbox Code Playgroud)