Skip to content

Commit 5495b59

Browse files
committed
Remove xarray_groupby_reduce
1 parent bc5aaf0 commit 5495b59

File tree

2 files changed

+2
-74
lines changed

2 files changed

+2
-74
lines changed

flox/xarray.py

+1-42
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
from .xrutils import is_duck_dask_array, isnull
1717

1818
if TYPE_CHECKING:
19-
from xarray import DataArray, Dataset, GroupBy, Resample
19+
from xarray import DataArray, Dataset, Resample
2020

2121

2222
def _get_input_core_dims(group_names, dim, ds, to_group):
@@ -403,47 +403,6 @@ def wrapper(array, to_group, *, func, skipna, **kwargs):
403403
return actual
404404

405405

406-
def xarray_groupby_reduce(
407-
groupby: "GroupBy",
408-
func: Union[str, Aggregation],
409-
split_out: int = 1,
410-
method: str = "map-reduce",
411-
keep_attrs: bool = True,
412-
):
413-
"""Apply on an existing Xarray groupby object for convenience."""
414-
415-
def wrapper(*args, **kwargs):
416-
result, _ = groupby_reduce(*args, **kwargs)
417-
return result
418-
419-
groups = list(groupby.groups.keys())
420-
outdim = groupby._unique_coord.name
421-
groupdim = groupby._group_dim
422-
423-
actual = xr.apply_ufunc(
424-
wrapper,
425-
groupby._obj,
426-
groupby._group,
427-
input_core_dims=[[groupdim], [groupdim]],
428-
# for xarray's test_groupby_duplicate_coordinate_labels
429-
exclude_dims=set(groupdim),
430-
output_core_dims=[[outdim]],
431-
dask="allowed",
432-
dask_gufunc_kwargs=dict(output_sizes={outdim: len(groups)}),
433-
keep_attrs=keep_attrs,
434-
kwargs={
435-
"func": func,
436-
"axis": -1,
437-
"split_out": split_out,
438-
"expected_groups": groups,
439-
"method": method,
440-
},
441-
)
442-
actual[outdim] = groups
443-
444-
return actual
445-
446-
447406
def rechunk_for_cohorts(
448407
obj: Union["DataArray", "Dataset"],
449408
dim: str,

tests/test_xarray.py

+1-32
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,7 @@
66
xr = pytest.importorskip("xarray")
77
# isort: on
88

9-
from flox.xarray import (
10-
rechunk_to_group_boundaries,
11-
resample_reduce,
12-
xarray_groupby_reduce,
13-
xarray_reduce,
14-
)
9+
from flox.xarray import rechunk_to_group_boundaries, resample_reduce, xarray_reduce
1510

1611
from . import assert_equal, engine, has_dask, raise_if_dask_computes, requires_dask
1712

@@ -71,32 +66,6 @@ def test_xarray_reduce(skipna, add_nan, min_count, engine):
7166
# assert_equal(expected, actual)
7267

7368

74-
def test_xarray_groupby_reduce():
75-
arr = np.ones((4, 12))
76-
77-
labels = np.array(["a", "a", "c", "c", "c", "b", "b", "c", "c", "b", "b", "f"])
78-
labels = np.array(labels)
79-
labels2 = np.array([1, 2, 2, 1])
80-
81-
da = xr.DataArray(
82-
arr, dims=("x", "y"), coords={"labels2": ("x", labels2), "labels": ("y", labels)}
83-
).expand_dims(z=4)
84-
85-
grouped = da.groupby("labels")
86-
expected = grouped.mean()
87-
actual = xarray_groupby_reduce(grouped, "mean")
88-
assert_equal(expected, actual)
89-
90-
actual = xarray_groupby_reduce(da.transpose("y", ...).groupby("labels"), "mean")
91-
assert_equal(expected, actual)
92-
93-
# TODO: fails because of stacking
94-
# grouped = da.groupby("labels2")
95-
# expected = grouped.mean()
96-
# actual = xarray_groupby_reduce(grouped, "mean")
97-
# assert_equal(expected, actual)
98-
99-
10069
def test_xarray_reduce_multiple_groupers():
10170
arr = np.ones((4, 12))
10271

0 commit comments

Comments
 (0)