如何更改dimensions xarray.dataset中的顺序?

发布于 2025-02-13 10:39:00 字数 1059 浏览 1 评论 0原文

我正在创建一个如下:

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

enter image description here

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

画中仙 2025-02-20 10:39:00

您可以通过选择两个列表来选择两个坐标和变量来重新排序坐标和变量:

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 ...

You can reorder your coordinates and variables by selecting them both in order using a list:

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 ...
金兰素衣 2025-02-20 10:39:00

如何更改Xarray的维度顺序:

在您的示例中,那将是:

rainfall_dataset['rainfall_depth'].T.transpose('time','x','y')

IE:

Dataset['Variable'].T.transpose('dim1','dim2','dim3)

必需:列出所需的所有维度订购。

如果您想在数据集中修改变量:

rainfall_dataset['rainfall_depth'] = rainfall_dataset['rainfall_depth'].T.transpose('time','x','y')

IE:

Dataset['Variable'] = Dataset['Variable'].T.transpose('dim1','dim2','dim3)

How to change the order of dimension of a xarray: transpose

In your example, that would be:

rainfall_dataset['rainfall_depth'].T.transpose('time','x','y')

i.e.:

Dataset['Variable'].T.transpose('dim1','dim2','dim3)

Required: List all dimensions in the desired order.

If you want a modification of your variable in your dataset:

rainfall_dataset['rainfall_depth'] = rainfall_dataset['rainfall_depth'].T.transpose('time','x','y')

i.e.:

Dataset['Variable'] = Dataset['Variable'].T.transpose('dim1','dim2','dim3)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文