Skip to content

Fix blockwise sort optimization #181

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

Merged
merged 1 commit into from
Oct 25, 2022
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
2 changes: 1 addition & 1 deletion flox/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -1760,7 +1760,7 @@ def groupby_reduce(
assert len(groups) == 1
sorted_idx = np.argsort(groups[0])
# This optimization helps specifically with resampling
if not (sorted_idx[1:] <= sorted_idx[:-1]).all():
if not (sorted_idx[:-1] <= sorted_idx[1:]).all():
result = result[..., sorted_idx]
groups = (groups[0][sorted_idx],)

Expand Down
20 changes: 20 additions & 0 deletions tests/test_core.py
Original file line number Diff line number Diff line change
Expand Up @@ -1183,3 +1183,23 @@ def test_validate_reindex():
for func in ["sum", "argmax"]:
actual = _validate_reindex(None, func, method, expected_groups=None, by_is_dask=False)
assert actual is False


@requires_dask
def test_1d_blockwise_sort_optimization():
# Make sure for resampling problems sorting isn't done.
time = pd.Series(pd.date_range("2020-09-01", "2020-12-31 23:59", freq="3H"))
array = dask.array.ones((len(time),), chunks=(224,))

actual, _ = groupby_reduce(array, time.dt.dayofyear.values, method="blockwise", func="count")
assert all("getitem" not in k for k in actual.dask)

actual, _ = groupby_reduce(
array, time.dt.dayofyear.values[::-1], sort=True, method="blockwise", func="count"
)
assert any("getitem" in k for k in actual.dask.layers)

actual, _ = groupby_reduce(
array, time.dt.dayofyear.values[::-1], sort=False, method="blockwise", func="count"
)
assert all("getitem" not in k for k in actual.dask.layers)