Inverting Datasets or DataArrays with dask float data throws wrong ValueError instead of TypeError
Nobody has claimed this yet.
- Dominant language
- Python
- Stars
- 4.2k
- Forks
- 1.4k
- Avg merge
- 2d 15h
- Merged PRs (30d)
- 14
Description
What happened?
Inverting (__invert__) a DataArray or Dataset containing dask float data throws a ValueError with a message about the dimensions not matching the data. This should throw a TypeError with a message indicating an unsupported type for that operation.
What did you expect to happen?
I expected a TypeError with a relevant message about the type (shown below), not a ValueError with a misleading message.
import xarray as xr
import dask.array as da
data = da.from_array([1.0])
x = xr.DataArray(data)
~x
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/home/USEr/anaconda3/envs/rstools/lib/python3.8/site-packages/xarray/core/_typed_ops.py", line 329, in __invert__
return self._unary_op(operator.invert)
File "/home/USER/anaconda3/envs/rstools/lib/python3.8/site-packages/xarray/core/dataarray.py", line 3078, in _unary_op
da = self.__array_wrap__(f(self.variable.data, *args, **kwargs))
TypeError: ufunc 'invert' not supported for the input types, and the inputs could not be safely coerced to any supported types according to the casting rule ''safe''
Minimal Complete Verifiable Example
import xarray as xr
import dask.array as da
data = da.from_array([1.0])
x = xr.DataArray(data)
~x
Relevant log output
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/home/USER/anaconda3/envs/rstools/lib/python3.8/site-packages/xarray/core/_typed_ops.py", line 329, in __invert__
return self._unary_op(operator.invert)
File "/home/USER/anaconda3/envs/rstools/lib/python3.8/site-packages/xarray/core/dataarray.py", line 3078, in _unary_op
da = self.__array_wrap__(f(self.variable.data, *args, **kwargs))
File "/home/USER/anaconda3/envs/rstools/lib/python3.8/site-packages/xarray/core/dataarray.py", line 3057, in __array_wrap__
new_var = self.variable.__array_wrap__(obj, context)
File "/home/USER/anaconda3/envs/rstools/lib/python3.8/site-packages/xarray/core/variable.py", line 2444, in __array_wrap__
return Variable(self.dims, obj)
File "/home/USER/anaconda3/envs/rstools/lib/python3.8/site-packages/xarray/core/variable.py", line 305, in __init__
self._dims = self._parse_dimensions(dims)
File "/home/USER/anaconda3/envs/rstools/lib/python3.8/site-packages/xarray/core/variable.py", line 573, in _parse_dimensions
raise ValueError(
ValueError: dimensions ('dim_0',) must have the same length as the number of data dimensions, ndim=0
Anything else we need to know?
No response
Environment
/home/USER/anaconda3/envs/rstools/lib/python3.8/site-packages/_distutils_hack/init.py:30: UserWarning: Setuptools is replacing distutils.
warnings.warn("Setuptools is replacing distutils.")
INSTALLED VERSIONS
commit: None
python: 3.8.13 | packaged by conda-forge | (default, Mar 25 2022, 06:04:10)
[GCC 10.3.0]
python-bits: 64
OS: Linux
OS-release: 5.13.0-35-generic
machine: x86_64
processor: x86_64
byteorder: little
LC_ALL: None
LANG: en_US.UTF-8
LOCALE: ('en_US', 'UTF-8')
libhdf5: None
libnetcdf: None
xarray: 2022.3.0
pandas: 1.4.1
numpy: 1.21.5
scipy: 1.8.0
netCDF4: None
pydap: None
h5netcdf: None
h5py: None
Nio: None
zarr: None
cftime: None
nc_time_axis: None
PseudoNetCDF: None
rasterio: 1.2.10
cfgrib: None
iris: None
bottleneck: None
dask: 2022.03.0
distributed: 2022.3.0
matplotlib: 3.5.1
cartopy: None
seaborn: None
numbagg: None
fsspec: 2022.02.0
cupy: None
pint: None
sparse: None
setuptools: 61.2.0
pip: 22.0.4
conda: None
pytest: 7.1.1
IPython: 8.2.0
sphinx: None
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Reproduce the minimal example, then inspect xarray/core/_typed_ops.py, dataarray.py, and variable.py, especially _unary_op and array_wrap shown in the traceback. Add or locate coverage for inverting dask-backed float data and verify that the operation raises a TypeError with a relevant unsupported-type message rather than the misleading dimensions ValueError.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- numpy, python
- Domain
- data
- Issue type
- Bug
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 48/100