It seems to me that Dask does more work than necessary when using DataFrame.groupby.
If I set up some data and want only a subset of the resulting operation,
import dask
df = dask.datasets.timeseries(partition_freq='2W').set_index('id').persist()
task = df.groupby('id')['x'].sum().head(compute=False)
Dask seems to want to use all partitions for the computation:
task.visualize(optimize_graph=True, size='8')
Groupby automatically collapses the dataframe into a single partition and I suppose head cannot change the graph retrospectively in that regards.
I hoped that perhaps split_out could help this, but that's also not the case.
task = df.groupby('id')['x'].sum(split_out=2).head(compute=False)
task.visualize(optimize_graph=True, size='8')
It therefore seems that in this particular case, Dask does (potentially significantly) more work than necessary.
Question
My questions thus are
- Is my assessment of the situation correct? Does Dask indeed do much more work? If not, how could I tell?
- If I'm correct, what can I do to tell Dask to only compute what is needed?
My actual problem is of course much more complex, but I'm hoping these toy examples will help illuminate the problems in my case.


