# Move dimensions to original order after apply_ufunc in xarray?

###### Posted By: Anonymous

My dataset has 3 dimensions in the order `(time, y, x)`

and I use `apply_ufunc`

to apply a computation along the `time`

dimension. This rearranges the order of the dimensions as `(y, x, time)`

. I need to restructure the xarray so its in the `(time, y, x)`

order as the original dataset. How would I go along doing this?

Here is a visual description of what’s happening:

*Before:*

Then I apply my function:

```
dcube = xr.apply_ufunc(
bc.clip_and_normalize_percentile,
dcube,
input_core_dims=[["time"]],
output_core_dims=[["time"]],
dask = 'allowed',
vectorize=True
)
```

as expected `time`

is moved to the last dimension:

How do I rearrange this so that its in the order of the original array? Are there parameters that prevent apply_ufunc from moving the dims?

## Solution

The docs say that

Core dimensions are automatically moved to the last axes of input

variables before applying func, which facilitates using NumPy style

generalized ufuncs

so it’s unlikely that there’s a way (or any parameters) to prevent that.

What I’ve been doing is simply call `.transpose`

afterwards to restore the initial order.

In your example, that would look like:

`dcube = dcube.transpose("time", ...)`

fixing `time`

to be the first dimension and shifting all other ones behind using `...`

.

###### Answered By: Anonymous

Disclaimer: This content is shared under creative common license cc-by-sa 3.0. It is generated from StackExchange Website Network.