Skip to content

.item() breaks on dataarray if dask arrays are (implicitely) used #7916

Open
@eiphione

Description

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

import xarray as xr

ds = xr.tutorial.open_dataset("air_temperature", chunks="auto")  # force dask usage but this can also happen implicitly

ds.air.mean().item()

MVCE confirmation

  • Minimal example — the example is as focused as reasonably possible to demonstrate the underlying issue in xarray.
  • Complete example — the example is self-contained, including all data and the text of any traceback.
  • Verifiable example — the example copy & pastes into an IPython prompt or Binder notebook, returning the result.
  • New issue — a search of GitHub Issues suggests this is not a duplicate.

Relevant log output

Traceback (most recent call last):
  File "/home/tschmitt/.cache/pypoetry/virtualenvs/xr-bug-ugHUypuW-py3.9/lib/python3.9/site-packages/xarray/core/ops.py", line 191, in _call_possibly_missing_method
    method = getattr(arg, name)
AttributeError: 'Array' object has no attribute 'item'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/home/tschmitt/software/xr_bug/mwe.py", line 6, in <module>
    ds.air.mean().item()
  File "/home/tschmitt/.cache/pypoetry/virtualenvs/xr-bug-ugHUypuW-py3.9/lib/python3.9/site-packages/xarray/core/ops.py", line 203, in func
    return _call_possibly_missing_method(self.data, name, args, kwargs)
  File "/home/tschmitt/.cache/pypoetry/virtualenvs/xr-bug-ugHUypuW-py3.9/lib/python3.9/site-packages/xarray/core/ops.py", line 193, in _call_possibly_missing_method
    duck_array_ops.fail_on_dask_array_input(arg, func_name=name)
  File "/home/tschmitt/.cache/pypoetry/virtualenvs/xr-bug-ugHUypuW-py3.9/lib/python3.9/site-packages/xarray/core/duck_array_ops.py", line 77, in fail_on_dask_array_input
    raise NotImplementedError(msg % func_name)
NotImplementedError: 'item' is not yet a valid method on dask arrays
Exception ignored in: <function CachingFileManager.__del__ at 0x7f8da8e1b1f0>
Traceback (most recent call last):
  File "/home/tschmitt/.cache/pypoetry/virtualenvs/xr-bug-ugHUypuW-py3.9/lib/python3.9/site-packages/xarray/backends/file_manager.py", line 246, in __del__
TypeError: 'NoneType' object is not callable

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

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions