Skip to content
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

Use ddof in numbagg>=0.7.0 for aggregations #8624

Merged
merged 6 commits into from
Jan 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 11 additions & 4 deletions xarray/core/nputils.py
Original file line number Diff line number Diff line change
Expand Up @@ -185,8 +185,12 @@ def f(values, axis=None, **kwargs):
and pycompat.mod_version("numbagg") >= Version("0.5.0")
and OPTIONS["use_numbagg"]
and isinstance(values, np.ndarray)
# numbagg uses ddof=1 only, but numpy uses ddof=0 by default
and (("var" in name or "std" in name) and kwargs.get("ddof", 0) == 1)
# numbagg<0.7.0 uses ddof=1 only, but numpy uses ddof=0 by default
and (
pycompat.mod_version("numbagg") >= Version("0.7.0")
or ("var" not in name and "std" not in name)
or kwargs.get("ddof", 0) == 1
)
# TODO: bool?
and values.dtype.kind in "uifc"
# and values.dtype.isnative
Expand All @@ -196,9 +200,12 @@ def f(values, axis=None, **kwargs):

nba_func = getattr(numbagg, name, None)
if nba_func is not None:
# numbagg does not take care dtype, ddof
# numbagg does not use dtype
kwargs.pop("dtype", None)
kwargs.pop("ddof", None)
# prior to 0.7.0, numbagg did not support ddof; we ensure it's limited
# to ddof=1 above.
if pycompat.mod_version("numbagg") < Version("0.7.0"):
kwargs.pop("ddof", None)
return nba_func(values, axis=axis, **kwargs)
if (
_BOTTLENECK_AVAILABLE
Expand Down
14 changes: 14 additions & 0 deletions xarray/tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import pandas as pd
import pytest

import xarray as xr
from xarray import DataArray, Dataset
from xarray.tests import create_test_data, requires_dask

Expand All @@ -13,6 +14,19 @@ def backend(request):
return request.param


@pytest.fixture(params=["numbagg", "bottleneck"])
def compute_backend(request):
if request.param == "bottleneck":
options = dict(use_bottleneck=True, use_numbagg=False)
elif request.param == "numbagg":
options = dict(use_bottleneck=False, use_numbagg=True)
else:
raise ValueError

with xr.set_options(**options):
yield request.param


@pytest.fixture(params=[1])
def ds(request, backend):
if request.param == 1:
Expand Down
13 changes: 0 additions & 13 deletions xarray/tests/test_rolling.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,19 +23,6 @@
]


@pytest.fixture(params=["numbagg", "bottleneck"])
def compute_backend(request):
if request.param == "bottleneck":
options = dict(use_bottleneck=True, use_numbagg=False)
elif request.param == "numbagg":
options = dict(use_bottleneck=False, use_numbagg=True)
else:
raise ValueError

with xr.set_options(**options):
yield request.param


@pytest.mark.parametrize("func", ["mean", "sum"])
@pytest.mark.parametrize("min_periods", [1, 10])
def test_cumulative(d, func, min_periods) -> None:
Expand Down
3 changes: 2 additions & 1 deletion xarray/tests/test_variable.py
Original file line number Diff line number Diff line change
Expand Up @@ -1754,7 +1754,8 @@ def test_reduce(self):
v.mean(dim="x", axis=0)

@requires_bottleneck
def test_reduce_use_bottleneck(self, monkeypatch):
@pytest.mark.parametrize("compute_backend", ["bottleneck"], indirect=True)
def test_reduce_use_bottleneck(self, monkeypatch, compute_backend):
def raise_if_called(*args, **kwargs):
raise RuntimeError("should not have been called")

Expand Down
Loading