How to change the order in dimensions xarray.Dataset?
Asked Answered
S

1

7

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)?

Selfsustaining answered 6/7, 2022 at 0:37 Comment(1)
related: #69745606Cheep
C
7

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 ...
Cheep answered 6/7, 2022 at 1:35 Comment(10)
Thanks @Michael, this way will change the shape of my rainfall_depth variable (from (time, y, x) to (time, x, y)), so it does not work for me unfortunately. As I mentioned, I wish to keep everything the same, only the order of in dimensions is changed.Selfsustaining
So sorry - I misread your question. Does simply calling transpose work?Cheep
Or perhaps transpose, followed by re-ordering the coordinates back to the original using this list method I describe?Cheep
Do you mean this rainfall_dataset.transpose()? If it is, it did not work either. It still changes the shape of my rainfall_depth variable and the order of dimensions are still the same.Selfsustaining
Sorry - I guess I don’t understand your question. Can you describe exactly what you want as a result?Cheep
It actually works when I tried as you suggested re-ordering the coordinates rainfall_dataset.transpose("time", "y", "x")[["time", "x", "y", "rainfall_depth"]]. And this is exactly what I am looking for. Thank you so much @Michael. If it's possible, you can change the answer and then I will upvote for it.Selfsustaining
Got it. Then you don’t actually need the transpose. Updated my answer.Cheep
For some reason this seems to drop all the data variables out of my dataset...Shelbashelbi
Hmm. It shouldn’t! Feel free to ask another question and post your code and results if you don’t figure it outCheep
the reorder list works for the "upper level". But how do i make the rainfall_depth DataArray follow the new order? ie rainfall_depth (time, x, y)?Melioration

© 2022 - 2024 — McMap. All rights reserved.