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

groupby: Don't set method by default on flox>=0.9 #8657

Merged
merged 2 commits into from
Jan 26, 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
3 changes: 3 additions & 0 deletions doc/whats-new.rst
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@ v2024.02.0 (unreleased)
New Features
~~~~~~~~~~~~

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

Breaking changes
~~~~~~~~~~~~~~~~
Expand Down
11 changes: 7 additions & 4 deletions xarray/core/groupby.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

import numpy as np
import pandas as pd
from packaging.version import Version

from xarray.core import dtypes, duck_array_ops, nputils, ops
from xarray.core._aggregations import (
Expand Down Expand Up @@ -1003,6 +1004,7 @@ def _flox_reduce(
**kwargs: Any,
):
"""Adaptor function that translates our groupby API to that of flox."""
import flox
from flox.xarray import xarray_reduce

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

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

numeric_only = kwargs.pop("numeric_only", None)
if numeric_only:
Expand Down
19 changes: 19 additions & 0 deletions xarray/tests/test_groupby.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,12 @@
import datetime
import operator
import warnings
from unittest import mock

import numpy as np
import pandas as pd
import pytest
from packaging.version import Version

import xarray as xr
from xarray import DataArray, Dataset, Variable
Expand Down Expand Up @@ -2499,3 +2501,20 @@ def test_groupby_dim_no_dim_equal(use_flox):
actual1 = da.drop_vars("lat").groupby("lat", squeeze=False).sum()
actual2 = da.groupby("lat", squeeze=False).sum()
assert_identical(actual1, actual2.drop_vars("lat"))


@requires_flox
def test_default_flox_method():
import flox.xarray

da = xr.DataArray([1, 2, 3], dims="x", coords={"label": ("x", [2, 2, 1])})

result = xr.DataArray([3, 3], dims="label", coords={"label": [1, 2]})
with mock.patch("flox.xarray.xarray_reduce", return_value=result) as mocked_reduce:
da.groupby("label").sum()

kwargs = mocked_reduce.call_args.kwargs
if Version(flox.__version__) < Version("0.9.0"):
assert kwargs["method"] == "cohorts"
else:
assert "method" not in kwargs
Loading