Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Raise nicer error if passing a list of dimension names to transpose #6502

Closed
fabienpaulot opened this issue Apr 20, 2022 · 5 comments · Fixed by #7120
Closed

Raise nicer error if passing a list of dimension names to transpose #6502

fabienpaulot opened this issue Apr 20, 2022 · 5 comments · Fixed by #7120

Comments

@fabienpaulot
Copy link

fabienpaulot commented Apr 20, 2022

What happened?

Hello,

in xarray 0.20.1, I am getting the following error

ds = xr.Dataset({"foo": (("x", "y", "z"), [[[42]]]), "bar": (("y", "z"), [[24]])})

ds.transpose("y", "z", "x")

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'

What did you expect to happen?

The expected result is

ds.transpose("y", "z", "x")

<xarray.Dataset>
Dimensions:  (x: 1, y: 1, z: 1)
Dimensions without coordinates: x, y, z
Data variables:
    foo      (y, z, x) int64 42
    bar      (y, z) int64 24

Minimal Complete Verifiable Example

No response

Relevant log output

No response

Anything else we need to know?

No response

Environment

INSTALLED VERSIONS

commit: None
python: 3.9.12 (main, Apr 5 2022, 06:56:58)
[GCC 7.5.0]
python-bits: 64
OS: Linux
OS-release: 3.10.0-1160.42.2.el7.x86_64
machine: x86_64
processor: x86_64
byteorder: little
LC_ALL: None
LANG: en_US
LOCALE: ('en_US', 'ISO8859-1')
libhdf5: 1.12.1
libnetcdf: 4.8.1

xarray: 0.20.1
pandas: 1.4.1
numpy: 1.21.5
scipy: 1.8.0
netCDF4: 1.5.7
pydap: None
h5netcdf: 999
h5py: 3.6.0
Nio: None
zarr: None
cftime: 1.5.1.1
nc_time_axis: 1.4.0
PseudoNetCDF: None
rasterio: None
cfgrib: None
iris: None
bottleneck: 1.3.4
dask: 2022.02.1
distributed: 2022.2.1
matplotlib: 3.5.1
cartopy: 0.18.0
seaborn: 0.11.2
numbagg: None
fsspec: 2022.02.0
cupy: None
pint: 0.18
sparse: 0.13.0
setuptools: 61.2.0
pip: 21.2.4
conda: None
pytest: None
IPython: 8.2.0
sphinx: None

@fabienpaulot fabienpaulot added bug needs triage Issue that has not been reviewed by xarray team member labels Apr 20, 2022
@dcherian
Copy link
Contributor

dcherian commented Apr 20, 2022

I can't reproduce on our dev branch. Can you try upgrading xarray please?

EDIT: can't reproduce on 2022.03.0 either.

@dcherian dcherian removed the needs triage Issue that has not been reviewed by xarray team member label Apr 20, 2022
@fabienpaulot
Copy link
Author

Thanks. I upgraded to 2022.03.0

I am still getting the error

Python 3.9.12 (main, Apr  5 2022, 06:56:58) 
[GCC 7.5.0] :: Anaconda, Inc. on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import xarray as xr
>>> xr.__version__
'2022.3.0'
>>> ds = xr.Dataset({"foo": (("x", "y", "z"), [[[42]]]), "bar": (("y", "z"), [[24]])})
>>> ds.transpose(['y','z','y'])
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/nbhome/f1p/miniconda3/envs/f1p_gfdl/lib/python3.9/site-packages/xarray/core/dataset.py", line 4650, in transpose
    _ = list(infix_dims(dims, self.dims, missing_dims))
  File "/nbhome/f1p/miniconda3/envs/f1p_gfdl/lib/python3.9/site-packages/xarray/core/utils.py", line 786, in infix_dims
    existing_dims = drop_missing_dims(dims_supplied, dims_all, missing_dims)
  File "/nbhome/f1p/miniconda3/envs/f1p_gfdl/lib/python3.9/site-packages/xarray/core/utils.py", line 874, in drop_missing_dims
    supplied_dims_set = {val for val in supplied_dims if val is not ...}
  File "/nbhome/f1p/miniconda3/envs/f1p_gfdl/lib/python3.9/site-packages/xarray/core/utils.py", line 874, in <setcomp>
    supplied_dims_set = {val for val in supplied_dims if val is not ...}
TypeError: unhashable type: 'list'

@dcherian
Copy link
Contributor

ds.transpose(['y','z','y'])

Ah... Reemove the list here and try ds.transpose("y", "z", x") (no list) which is what you have in the first post.

@fabienpaulot
Copy link
Author

Oh... I am so sorry about this. This works as expected now.
It's weird that using list seemed to have worked at some point. Thanks a lot for your help

@dcherian dcherian changed the title Error with transpose Raise nicer error if passing a list of dimension names to transpose Apr 20, 2022
@dcherian
Copy link
Contributor

I think we should raise a nicer error message. Transpose is an outlier in our API. In nearly every other function, you are expected to pass a list of dimension names.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants