pydata / pydata/xarray

Maximum recursion calling `np.divide` with `DataArray` as `where` argument

Open
#7,965 2 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

bug topic-arrays
Dominant language
Python
Stars
4.2k
Forks
1.4k
Avg merge
2d 15h
Merged PRs (30d)
14

Description

What happened?

Calling np.divide() passing a bool expression involving a DataArray results in a RecursionError starting at apply_ufunc. Passing DataArray.data works fine.

What did you expect to happen?

No response

Minimal Complete Verifiable Example
import numpy as np
import xarray as xr

N = 10
array = np.random.random((N,))
da = xr.DataArray(np.random.random((N,)), dims=("x",), coords={"x": np.arange(N)})

# This works
print(np.divide(array, da, where=(da > 0.2).data))

# This gives recursion error
print(np.divide(array, da, where=da > 0.2))
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 "divide_recursion_mvce.py", line 7, in <module>
    print(np.divide(array, da, where=da > 0.2))
          ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "venv/lib64/python3.11/site-packages/xarray/core/arithmetic.py", line 86, in __array_ufunc__
    return apply_ufunc(
           ^^^^^^^^^^^^
  File "venv/lib64/python3.11/site-packages/xarray/core/computation.py", line 1197, in apply_ufunc
    return apply_dataarray_vfunc(
           ^^^^^^^^^^^^^^^^^^^^^^
  File "venv/lib64/python3.11/site-packages/xarray/core/computation.py", line 304, in apply_dataarray_vfunc
    result_var = func(*data_vars)
                 ^^^^^^^^^^^^^^^^
  File "venv/lib64/python3.11/site-packages/xarray/core/computation.py", line 761, in apply_variable_ufunc
    result_data = func(*input_data)
                  ^^^^^^^^^^^^^^^^^
  File "venv/lib64/python3.11/site-packages/xarray/core/arithmetic.py", line 86, in __array_ufunc__
    return apply_ufunc(
           ^^^^^^^^^^^^
  File "venv/lib64/python3.11/site-packages/xarray/core/computation.py", line 1210, in apply_ufunc
    return apply_array_ufunc(func, *args, dask=dask)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

...

  File "venv/lib64/python3.11/site-packages/xarray/core/computation.py", line 1210, in apply_ufunc
    return apply_array_ufunc(func, *args, dask=dask)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "venv/lib64/python3.11/site-packages/xarray/core/computation.py", line 815, in apply_array_ufunc
    if any(is_chunked_array(arg) for arg in args):
       ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "venv/lib64/python3.11/site-packages/xarray/core/computation.py", line 815, in <genexpr>
    if any(is_chunked_array(arg) for arg in args):
           ^^^^^^^^^^^^^^^^^^^^^
  File "venv/lib64/python3.11/site-packages/xarray/core/pycompat.py", line 99, in is_chunked_array
    return is_duck_dask_array(x) or (is_duck_array(x) and hasattr(x, "chunks"))
           ^^^^^^^^^^^^^^^^^^^^^
  File "venv/lib64/python3.11/site-packages/xarray/core/pycompat.py", line 95, in is_duck_dask_array
    return is_duck_array(x) and is_dask_collection(x)
                                ^^^^^^^^^^^^^^^^^^^^^
  File "venv/lib64/python3.11/site-packages/xarray/core/pycompat.py", line 87, in is_dask_collection
    if module_available("dask"):
       ^^^^^^^^^^^^^^^^^^^^^^^^
  File "venv/lib64/python3.11/site-packages/xarray/core/utils.py", line 1160, in module_available
    return importlib.util.find_spec(module) is not None
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "<frozen importlib.util>", line 103, in find_spec
  File "<frozen importlib._bootstrap>", line 1080, in _find_spec
  File "<frozen importlib._bootstrap>", line 924, in find_spec
  File "<frozen importlib._bootstrap>", line 241, in _call_with_frames_removed
RecursionError: maximum recursion depth exceeded while calling a Python object
Anything else we need to know?

No response

Environment
INSTALLED VERSIONS ------------------ commit: None python: 3.11.3 (main, Apr 27 2023, 22:08:21) [GCC] python-bits: 64 OS: Linux OS-release: 6.3.9-1-default machine: x86_64 processor: x86_64 byteorder: little LC_ALL: None LANG: en_GB.UTF-8 LOCALE: ('en_GB', 'UTF-8') libhdf5: 1.12.2 libnetcdf: 4.9.3-development

xarray: 2023.6.0
pandas: 1.5.3
numpy: 1.25.0
scipy: 1.11.1
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: None
distributed: None
matplotlib: 3.7.2
cartopy: None
seaborn: None
numbagg: None
fsspec: None
cupy: None
pint: None
sparse: None
flox: None
numpy_groupies: None
setuptools: 65.5.0
pip: 23.1.2
conda: None
pytest: 7.4.0
mypy: None
IPython: 8.14.0
sphinx: None

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Run the minimal example and compare np.divide with DataArray as where against where=(da > 0.2).data. Read xarray/core/arithmetic.py and the apply_ufunc paths in xarray/core/computation.py shown in the traceback. Done means the DataArray where form no longer recurses and has regression coverage for the reproduced case.

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
Clearly specified
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.