-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
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
.item() breaks on dataarray if dask arrays are (implicitely) used #7916
Comments
Thanks for opening your first issue here at xarray! Be sure to follow the issue template! |
Thanks for reporting this. Here's an even simpler example reproducing the error: In [2]: da = xr.DataArray([2])
In [3]: da.item()
Out[3]: 2
In [4]: da.chunk().item()
---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
File ~/Documents/Work/Code/xarray/xarray/core/ops.py:193, in _call_possibly_missing_method(arg, name, args, kwargs)
192 try:
--> 193 method = getattr(arg, name)
194 except AttributeError:
AttributeError: 'Array' object has no attribute 'item'
During handling of the above exception, another exception occurred:
NotImplementedError Traceback (most recent call last)
Cell In[4], line 1
----> 1 da.chunk().item()
File ~/Documents/Work/Code/xarray/xarray/core/ops.py:205, in _values_method_wrapper.<locals>.func(self, *args, **kwargs)
204 def func(self, *args, **kwargs):
--> 205 return _call_possibly_missing_method(self.data, name, args, kwargs)
File ~/Documents/Work/Code/xarray/xarray/core/ops.py:195, in _call_possibly_missing_method(arg, name, args, kwargs)
193 method = getattr(arg, name)
194 except AttributeError:
--> 195 duck_array_ops.fail_on_dask_array_input(arg, func_name=name)
196 if hasattr(arg, "data"):
197 duck_array_ops.fail_on_dask_array_input(arg.data, func_name=name)
File ~/Documents/Work/Code/xarray/xarray/core/duck_array_ops.py:79, in fail_on_dask_array_input(values, msg, func_name)
77 if func_name is None:
78 func_name = inspect.stack()[1][3]
---> 79 raise NotImplementedError(msg % func_name)
NotImplementedError: 'item' is not yet a valid method on dask arrays You've managed to find a piece of what I think is pretty rarely-used code - can I ask why you're using The error here is indicating the fact that the The question is whether calling |
IIUC |
Right - what I'm saying is that maybe xarray should do that compute for you. |
Thanks for the quick reply. We also figured out, that .compute().item() works in the specific case, but also came to @TomNicholas conclusion, that most likely this should be handled in the background, so that the API does not break unexpectedly. Personally I am pretty new to how xarray actually works "under the hood", so I can only speak from a the point of view of a user that most of the timer did not bother about what type of array is actually holding the data. |
+1 to this suggestion. I've been bitten by this many times actually, where I want to extract a simple python scalar with |
I'm not too sure. I think we should defer to the underlying array type since they provide
cc @shoyer |
Adding From the xarray side the |
I don't think it's a great idea for Xarray operations to implicit compute Dask arrays. We've been very careful to avoid this in the past, because it can result in huge and hard to tract down performance penalties. I agree with @dcherian and @slevang. We should consider implementing |
So should this be done independantly of the open PR for |
What happened?
When using .item() to get a value (for example of a calculated average via .mean()) of an dataarray, this results in an error if the underlying array is a dask array.
What did you expect to happen?
I did not explicitely use dask, therefore I expected .item() to work. If dask arrays can be used implicitely, then I would expect the API to work, regardless of the underlying array data type.
Minimal Complete Verifiable Example
MVCE confirmation
Relevant log output
Anything else we need to know?
No response
Environment
INSTALLED VERSIONS
commit: None
python: 3.9.12 | packaged by conda-forge | (main, Mar 24 2022, 23:22:55)
[GCC 10.3.0]
python-bits: 64
OS: Linux
OS-release: 3.10.0-862.14.4.el7.x86_64
machine: x86_64
processor: x86_64
byteorder: little
LC_ALL: None
LANG: en_US.UTF-8
LOCALE: ('en_US', 'UTF-8')
libhdf5: 1.12.2
libnetcdf: 4.9.3-development
xarray: 2023.4.0
pandas: 2.0.2
numpy: 1.24.3
scipy: None
netCDF4: 1.6.4
pydap: None
h5netcdf: None
h5py: None
Nio: None
zarr: None
cftime: 1.6.2
nc_time_axis: None
PseudoNetCDF: None
iris: None
bottleneck: None
dask: 2023.6.0
distributed: None
matplotlib: None
cartopy: None
seaborn: None
numbagg: None
fsspec: 2023.6.0
cupy: None
pint: None
sparse: None
flox: None
numpy_groupies: None
setuptools: 62.3.2
pip: 22.1.1
conda: None
pytest: None
mypy: None
IPython: None
sphinx: None
The text was updated successfully, but these errors were encountered: