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

Recognize grouping by IntervalIndex as binning #205

Merged
merged 3 commits into from
Jan 19, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
9 changes: 7 additions & 2 deletions flox/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -1559,7 +1559,10 @@ def _validate_expected_groups(nby: int, expected_groups: T_ExpectedGroupsOpt) ->
return (None,) * nby

if nby == 1 and not isinstance(expected_groups, tuple):
return (np.asarray(expected_groups),)
if isinstance(expected_groups, pd.Index):
return (expected_groups,)
else:
return (np.asarray(expected_groups),)

if nby > 1 and not isinstance(expected_groups, tuple): # TODO: test for list
raise ValueError(
Expand Down Expand Up @@ -1734,9 +1737,11 @@ def groupby_reduce(
# (pd.IntervalIndex or not)
expected_groups = _convert_expected_groups_to_index(expected_groups, isbins, sort)

is_binning = any(isbins) or any([isinstance(e, pd.IntervalIndex) for e in expected_groups])
dcherian marked this conversation as resolved.
Show resolved Hide resolved

# TODO: could restrict this to dask-only
factorize_early = (nby > 1) or (
any(isbins) and method == "cohorts" and is_duck_dask_array(array)
is_binning and method == "cohorts" and is_duck_dask_array(array)
)
if factorize_early:
bys, final_groups, grp_shape = _factorize_multiple(
Expand Down
4 changes: 3 additions & 1 deletion flox/xarray.py
Original file line number Diff line number Diff line change
Expand Up @@ -313,7 +313,9 @@ def xarray_reduce(
group_names: tuple[Any, ...] = ()
group_sizes: dict[Any, int] = {}
for idx, (b_, expect, isbin_) in enumerate(zip(by_da, expected_groups, isbins)):
group_name = b_.name if not isbin_ else f"{b_.name}_bins"
group_name = (
f"{b_.name}_bins" if isbin_ or isinstance(expect, pd.IntervalIndex) else b_.name
)
group_names += (group_name,)

if isbin_ and isinstance(expect, int):
Expand Down
15 changes: 13 additions & 2 deletions tests/test_xarray.py
Original file line number Diff line number Diff line change
Expand Up @@ -457,7 +457,8 @@ def test_datetime_array_reduce(use_cftime, func, engine):


@requires_dask
def test_groupby_bins_indexed_coordinate():
@pytest.mark.parametrize("method", ["cohorts", "map-reduce"])
def test_groupby_bins_indexed_coordinate(method):
ds = (
xr.tutorial.open_dataset("air_temperature")
.isel(time=slice(100))
Expand All @@ -472,7 +473,17 @@ def test_groupby_bins_indexed_coordinate():
expected_groups=([40, 50, 60, 70],),
isbin=(True,),
func="mean",
method="split-reduce",
method=method,
)
xr.testing.assert_allclose(expected, actual)

actual = xarray_reduce(
ds,
ds.lat,
dim=ds.air.dims,
expected_groups=pd.IntervalIndex.from_breaks([40, 50, 60, 70]),
func="mean",
method=method,
)
xr.testing.assert_allclose(expected, actual)

Expand Down