Skip to content

Commit a0b9d1f

Browse files
committed
Fix binning by nD variable
1 parent 759694b commit a0b9d1f

File tree

2 files changed

+22
-1
lines changed

2 files changed

+22
-1
lines changed

flox/core.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -462,7 +462,7 @@ def factorize_(
462462
group_idx = factorized[0]
463463

464464
if fastpath:
465-
return group_idx, found_groups, grp_shape
465+
return group_idx.reshape(by[0].shape), found_groups, grp_shape
466466

467467
if np.isscalar(axis) and groupvar.ndim > 1:
468468
# Not reducing along all dimensions of by

tests/test_xarray.py

+21
Original file line numberDiff line numberDiff line change
@@ -430,3 +430,24 @@ def test_datetime_array_reduce(use_cftime, func):
430430
expected = getattr(time.resample(time="YS"), func)()
431431
actual = resample_reduce(time.resample(time="YS"), func=func, engine="flox")
432432
assert_equal(expected, actual)
433+
434+
435+
@requires_dask
436+
def test_groupby_bins_indexed_coordinate():
437+
ds = (
438+
xr.tutorial.open_dataset("air_temperature")
439+
.isel(time=slice(100))
440+
.chunk({"time": 20, "lat": 5})
441+
)
442+
bins = [40, 50, 60, 70]
443+
expected = ds.groupby_bins("lat", bins=bins).mean(keep_attrs=True, dim=...)
444+
actual = xarray_reduce(
445+
ds,
446+
ds.lat,
447+
dim=ds.air.dims,
448+
expected_groups=([40, 50, 60, 70],),
449+
isbin=(True,),
450+
func="mean",
451+
method="split-reduce",
452+
)
453+
xr.testing.assert_allclose(expected, actual)

0 commit comments

Comments
 (0)