Skip to content

Fix infinite recursion when calling np.fix #10248

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

Merged
merged 13 commits into from
Apr 28, 2025
Merged
7 changes: 3 additions & 4 deletions xarray/computation/apply_ufunc.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,7 @@
from xarray.core.formatting import limit_lines
from xarray.core.indexes import Index, filter_indexes_from_coords
from xarray.core.options import _get_keep_attrs
from xarray.core.utils import (
is_dict_like,
result_name,
)
from xarray.core.utils import is_dict_like, result_name
from xarray.core.variable import Variable
from xarray.namedarray.parallelcompat import get_chunked_array_type
from xarray.namedarray.pycompat import is_chunked_array
Expand Down Expand Up @@ -1212,6 +1209,8 @@ def apply_ufunc(
dask_gufunc_kwargs.setdefault("output_sizes", output_sizes)

if kwargs:
if "where" in kwargs and isinstance(kwargs["where"], DataArray):
kwargs["where"] = kwargs["where"].data # type:ignore[index]
func = functools.partial(func, **kwargs)

if keep_attrs is None:
Expand Down
11 changes: 11 additions & 0 deletions xarray/tests/test_computation.py
Original file line number Diff line number Diff line change
Expand Up @@ -2627,3 +2627,14 @@ def test_complex_number_reduce(compute_backend):
# Check that xarray doesn't call into numbagg, which doesn't compile for complex
# numbers at the moment (but will when numba supports dynamic compilation)
da.min()


def test_fix() -> None:
val = 3.0
val_fixed = np.fix(val)

da = xr.DataArray([val])
expected = xr.DataArray([val_fixed])

actual = np.fix(da)
assert_identical(expected, actual)
Loading