Reputation: 478
Suppose I have a Dataset
with a few coordinates and two of them, say 'x'
and 'y'
, are the same length. Now, if I have a variable in the Dataset
that has many coordinates and x
is one them, how can I change that coordinate to be y
in an easy way?
Upvotes: 2
Views: 1892
Reputation: 121
Xarray's Dataset object has a built-in method to do exactly this: Dataset.swap_dims
. The documentation is here: http://xarray.pydata.org/en/stable/generated/xarray.Dataset.swap_dims.html
In your case, with ds as your Dataset
,
ds.swap_dims({'x': 'y'}, inplace=True)
will return your Dataset with the x and y dimensions swapped in all data variables. Or if you want to keep your original Dataset,
ds_new = ds.swap_dims({'x', 'y'})
will store the Dataset
with the dimensions swapped in dataset_new
while leaving the original Dataset
unchanged.
Upvotes: 2