'xarray transpose: TypeError: unhashable type: 'list'

I am trying to rearrange the dimensions of the following dataset

X 
xarray.Dataset

    Dimensions:
        lon: 720lat: 360sector: 8time: 240
    Coordinates:
        lon
        (lon)
        float64
        -179.8 -179.2 ... 179.2 179.8
        lat
        (lat)
        float64
        -89.75 -89.25 ... 89.25 89.75
        sector
        (sector)
        int32
        0 1 2 3 4 5 6 7
        time
        (time)
        object
        2000-01-16 00:00:00 ... 2019-12-...
    Data variables:
        CO_em_anthro
        (time, sector, lat, lon)
        float32
        0.0 0.0 0.0 0.0 ... 0.0 0.0 0.0 0.0
    Attributes: (33)

This used to work

X.transpose(['lat','lon','sector','time'])

but in version 0.20.1, I am getting the following error

File /nbhome/f1p/miniconda3/envs/f1p_gfdl/lib/python3.9/site-packages/xarray/core/utils.py:879, in drop_missing_dims(supplied_dims, dims, missing_dims)
    868 """Depending on the setting of missing_dims, drop any dimensions from supplied_dims that
    869 are not present in dims.
    870 
   (...)
    875 missing_dims : {"raise", "warn", "ignore"}
    876 """
    878 if missing_dims == "raise":
--> 879     supplied_dims_set = {val for val in supplied_dims if val is not ...}
    880     invalid = supplied_dims_set - set(dims)
    881     if invalid:

File /nbhome/f1p/miniconda3/envs/f1p_gfdl/lib/python3.9/site-packages/xarray/core/utils.py:879, in <setcomp>(.0)
    868 """Depending on the setting of missing_dims, drop any dimensions from supplied_dims that
    869 are not present in dims.
    870 
   (...)
    875 missing_dims : {"raise", "warn", "ignore"}
    876 """
    878 if missing_dims == "raise":
--> 879     supplied_dims_set = {val for val in supplied_dims if val is not ...}
    880     invalid = supplied_dims_set - set(dims)
    881     if invalid:

TypeError: unhashable type: 'list'

calling transpose does work without the dimension name. I am not sure how to fix this issue. Thanks



Solution 1:[1]

Xarray’s transpose accepts the target dimensions as multiple arguments, not a list of dimensions.

See the *args in the transpose docs.

You need to change your code to:

X.transpose('lat','lon','sector','time')

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Michael Delgado