如何更改dimensions xarray.dataset中的顺序?
我正在创建一个如下:
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)
)
)
结果类似于
和我运行时的尺寸rainfall_example.dims
就像冷冻({'time':120,'y':1331,'x':1488})
(也可以在上述结果)。我知道xarray.dataset.dims
不能根据在这里
我的问题是:我们如何将这些维度的顺序更改为这样的维度冷冻({'time':120,'x':1488,'y':1331})
而无需更改其他任何内容(一切都将相同,只有更改维度的顺序)?
I am creating a xarray dataset as below:
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)
)
)
The results are like this
And the dimensions when I run rainfall_example.dims
are like this Frozen({'time': 120, 'y': 1331, 'x': 1488})
(this can also be seen in the above results). I know the xarray.Dataset.dims
cannot be modified according to here
My question is: How can we change the order of those dimensions into the dimensions like this Frozen({'time': 120, 'x': 1488, 'y': 1331})
without changing anything else (everything will be the same only the order in dimensions is changed)?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以通过选择两个列表来选择两个坐标和变量来重新排序坐标和变量:
You can reorder your coordinates and variables by selecting them both in order using a list:
如何更改Xarray的维度顺序:
在您的示例中,那将是:
IE:
必需:列出所需的所有维度订购。
如果您想在数据集中修改变量:
IE:
How to change the order of dimension of a xarray: transpose
In your example, that would be:
i.e.:
Required: List all dimensions in the desired order.
If you want a modification of your variable in your dataset:
i.e.: