Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to sort the coordinates and variables of an xr.Dataset

I would like to sort the coordinates and variables of an xarray Dataset in alphabetical order. I have tried to do this using ds.transpose(*sorted(ds.dims)). This seems to sort the coordinates/dimensions of each DataArray in the Dataset, but not the coordinates of the Dataset itself.

Example:

>>> ds = xr.Dataset(
...     {
...         'z': (['c', 'a', 'b'], np.ones(shape=(2, 2, 2))),
...         'x': (['a', 'b', 'c'], np.zeros(shape=(2, 2, 2))),
...         'y': (['c'], [0, 1]),
...     },
...     coords={'c': [30, 31], 'a': [10, 11], 'b': [20, 21]}
... )
    
>>> ds.transpose('a', 'b', 'c')
<xarray.Dataset>
Dimensions:  (c: 2, a: 2, b: 2)
Coordinates:
  * c        (c) int64 30 31
  * a        (a) int64 10 11
  * b        (b) int64 20 21
Data variables:
    z        (a, b, c) float64 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0
    x        (a, b, c) float64 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0
    y        (c) int64 0 1

Expected behaviour is that the coordinates and dimensions of the entire xr.Dataset would be sorted as 'a', 'b', 'c'. However, you can see that only the dimensions of the data variables themselves are in this order.

How can I sort the coordinates and variables of a Dataset, not just the array dimensions?

Any help is deeply appreciated, thank you!

like image 474
kit Avatar asked Oct 27 '21 20:10

kit


People also ask

How do I access Xarray data?

xarray offers extremely flexible indexing routines that combine the best features of NumPy and pandas for data selection. The most basic way to access elements of a DataArray object is to use Python's [] syntax, such as array[i, j] , where i and j are both integers.

What is Python Xarray?

Xarray is a python package for working with labeled multi-dimensional (a.k.a. N-dimensional, ND) arrays, it includes functions for advanced analytics and visualization. Xarray is heavily inspired by pandas and it uses pandas internally.


1 Answers

You can do this in xarray with xr.Dataset.reindex_like and xr.Dataset.reindex. The former is helpful if you have another object that you want to match.

ds = xr.Dataset(
data_vars={
'z': (['c', 'a', 'b'], np.ones(shape=(2, 2, 2))),
'x': (['a', 'b', 'c'], np.zeros(shape=(2, 2, 2))),
'y': (['c'], [0, 1]),
},
coords={'c': [30, 31], 'a': [10, 11], 'b': [20, 21]}
)

current_indexes = ds.indexes
desired_order = ['a', 'b', 'c']
reordered_indexes = {index_name: current_indexes[index_name] for index_name in desired_order}
reordered_ds = ds.reindex(reordered_indexes)
>>> ds
<xarray.Dataset>
Dimensions:  (c: 2, a: 2, b: 2)
Coordinates:
  * c        (c) int64 30 31
  * a        (a) int64 10 11
  * b        (b) int64 20 21
Data variables:
    z        (c, a, b) float64 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0
    x        (a, b, c) float64 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0
    y        (c) int64 0 1
>>> reordered_ds
<xarray.Dataset>
Dimensions:  (a: 2, b: 2, c: 2)
Coordinates:
  * a        (a) int64 10 11
  * b        (b) int64 20 21
  * c        (c) int64 30 31
Data variables:
    z        (c, a, b) float64 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0
    x        (a, b, c) float64 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0
    y        (c) int64 0 1
like image 161
Connor Dibble Avatar answered Oct 18 '22 04:10

Connor Dibble