Search code examples
pythonpython-xarray

How to get the shape of a xarray dataset by using dims labels


Is there a more elegant way than this to get a tuple of the shape of some dimensions of an xarray dataset?

tuple(dict(ds[['X', 'Y']].dims).values())

Solution

  • For an xarray.Dataset, you can do this:

    tuple(ds.dims[d] for d in ['X', 'Y'])
    

    This has the advantage of not subsetting the dataset or relying on the dimensions to be coordinates as well.