Skip to content

Commit ca4f121

Browse files
authored
groupby: Don't set method by default on flox>=0.9 (#8657)
* groupby: Set method="None" by default on flox>=0.9 * Fix?
1 parent 8704501 commit ca4f121

File tree

3 files changed

+29
-4
lines changed

3 files changed

+29
-4
lines changed

doc/whats-new.rst

+3
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,9 @@ v2024.02.0 (unreleased)
2323
New Features
2424
~~~~~~~~~~~~
2525

26+
- Xarray now defers to flox's `heuristics <https://flox.readthedocs.io/en/latest/implementation.html#heuristics>`_
27+
to set default `method` for groupby problems. This only applies to ``flox>=0.9``.
28+
By `Deepak Cherian <https://github.com/dcherian>`_.
2629

2730
Breaking changes
2831
~~~~~~~~~~~~~~~~

xarray/core/groupby.py

+7-4
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
import numpy as np
1818
import pandas as pd
19+
from packaging.version import Version
1920

2021
from xarray.core import dtypes, duck_array_ops, nputils, ops
2122
from xarray.core._aggregations import (
@@ -1003,6 +1004,7 @@ def _flox_reduce(
10031004
**kwargs: Any,
10041005
):
10051006
"""Adaptor function that translates our groupby API to that of flox."""
1007+
import flox
10061008
from flox.xarray import xarray_reduce
10071009

10081010
from xarray.core.dataset import Dataset
@@ -1014,10 +1016,11 @@ def _flox_reduce(
10141016
if keep_attrs is None:
10151017
keep_attrs = _get_keep_attrs(default=True)
10161018

1017-
# preserve current strategy (approximately) for dask groupby.
1018-
# We want to control the default anyway to prevent surprises
1019-
# if flox decides to change its default
1020-
kwargs.setdefault("method", "cohorts")
1019+
if Version(flox.__version__) < Version("0.9"):
1020+
# preserve current strategy (approximately) for dask groupby
1021+
# on older flox versions to prevent surprises.
1022+
# flox >=0.9 will choose this on its own.
1023+
kwargs.setdefault("method", "cohorts")
10211024

10221025
numeric_only = kwargs.pop("numeric_only", None)
10231026
if numeric_only:

xarray/tests/test_groupby.py

+19
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,12 @@
33
import datetime
44
import operator
55
import warnings
6+
from unittest import mock
67

78
import numpy as np
89
import pandas as pd
910
import pytest
11+
from packaging.version import Version
1012

1113
import xarray as xr
1214
from xarray import DataArray, Dataset, Variable
@@ -2499,3 +2501,20 @@ def test_groupby_dim_no_dim_equal(use_flox):
24992501
actual1 = da.drop_vars("lat").groupby("lat", squeeze=False).sum()
25002502
actual2 = da.groupby("lat", squeeze=False).sum()
25012503
assert_identical(actual1, actual2.drop_vars("lat"))
2504+
2505+
2506+
@requires_flox
2507+
def test_default_flox_method():
2508+
import flox.xarray
2509+
2510+
da = xr.DataArray([1, 2, 3], dims="x", coords={"label": ("x", [2, 2, 1])})
2511+
2512+
result = xr.DataArray([3, 3], dims="label", coords={"label": [1, 2]})
2513+
with mock.patch("flox.xarray.xarray_reduce", return_value=result) as mocked_reduce:
2514+
da.groupby("label").sum()
2515+
2516+
kwargs = mocked_reduce.call_args.kwargs
2517+
if Version(flox.__version__) < Version("0.9.0"):
2518+
assert kwargs["method"] == "cohorts"
2519+
else:
2520+
assert "method" not in kwargs

0 commit comments

Comments
 (0)