27
votes

The documentation for Dask talks about repartioning to reduce overhead here.

They however seem to indicate you need some knowledge of what your dataframe will look like beforehand (ie that there will 1/100th of the data expected).

Is there a good way to repartition sensibly without making assumptions? At the moment I just repartition with npartitions = ncores * magic_number, and set force to True to expand partitions if need be. This one size fits all approach works but is definitely suboptimal as my dataset varies in size.

The data is time series data, but unfortunately not at regular intervals, I've used repartition by time frequency in the past but this would be suboptimal because of how irregular the data is (sometimes nothing for minutes then thousands in seconds)

3
This may be a terrible idea - but would calling len() on df[df.name == 'Alice'] be too expensive? I feel like it shouldn't be - each worker sums up their dataframe length and the scheduler reduces that to a sum. Then, once you have that number, you can create a ratio of the original height to the current, and update the partition count via repartition accordingly. - kuanb
I think calling len() performs the entire task graph to that point, which would be extremely expensive. - Samantha Hughes
What I am trying currently is to compute() the dataframe to pandas after filtering. Then immediately stuff it back into a dask dataframe using the chunksize parameter on .from_pandas set to what I think is appropriate. This only works on dataframes that can fit in memory but does save expensive recomputes you'd get with len() - Samantha Hughes
Would not using something like count() on you subset query get you the length without needing load it into memory as a Pandas DataFrame and then resend it back to Dask? Seems like that operation itself is intense? - kuanb
There is no automatic way to repartition sensibly, although there probably should be. I might aim to have each dataframe be about 100MB in size. You could call df.memory_usage().sum().compute() to help determine a good number of partitions. - MRocklin

3 Answers

23
votes

As of Dask 2.0.0 you may call .repartition(partition_size="100MB").

This method performs an object-considerate (.memory_usage(deep=True)) breakdown of partition size. It will join smaller partitions, or split partitions that have grown too large.

Dask's Documentation also outlines the usage.

8
votes

Just to add to Samantha Hughes' answer:

memory_usage() by default ignores memory consumption of object dtype columns. For the datasets I have been working with recently this leads to an underestimate of memory usage of about 10x.

Unless you are sure there are no object dtype columns I would suggest specifying deep=True, that is, repartition using:

df.repartition(npartitions= 1+df.memory_usage(deep=True).sum().compute() // n )

Where n is your target partition size in bytes. Adding 1 ensures the number of partitions is always greater than 1 (// performs floor division).

7
votes

After discussion with mrocklin a decent strategy for partitioning is to aim for 100MB partition sizes guided by df.memory_usage().sum().compute(). With datasets that fit in RAM the additional work this might involve can be mitigated with use of df.persist() placed at relevant points.