Skip to content

Commit

Permalink
Fix binning by nD variable
Browse files Browse the repository at this point in the history
  • Loading branch information
dcherian committed Jun 5, 2022
1 parent 759694b commit a0b9d1f
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 1 deletion.
2 changes: 1 addition & 1 deletion flox/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -462,7 +462,7 @@ def factorize_(
group_idx = factorized[0]

if fastpath:
return group_idx, found_groups, grp_shape
return group_idx.reshape(by[0].shape), found_groups, grp_shape

if np.isscalar(axis) and groupvar.ndim > 1:
# Not reducing along all dimensions of by
Expand Down
21 changes: 21 additions & 0 deletions tests/test_xarray.py
Original file line number Diff line number Diff line change
Expand Up @@ -430,3 +430,24 @@ def test_datetime_array_reduce(use_cftime, func):
expected = getattr(time.resample(time="YS"), func)()
actual = resample_reduce(time.resample(time="YS"), func=func, engine="flox")
assert_equal(expected, actual)


@requires_dask
def test_groupby_bins_indexed_coordinate():
ds = (
xr.tutorial.open_dataset("air_temperature")
.isel(time=slice(100))
.chunk({"time": 20, "lat": 5})
)
bins = [40, 50, 60, 70]
expected = ds.groupby_bins("lat", bins=bins).mean(keep_attrs=True, dim=...)
actual = xarray_reduce(
ds,
ds.lat,
dim=ds.air.dims,
expected_groups=([40, 50, 60, 70],),
isbin=(True,),
func="mean",
method="split-reduce",
)
xr.testing.assert_allclose(expected, actual)

0 comments on commit a0b9d1f

Please sign in to comment.