Repository navigation
Description
Calling df.set_index('foo') currently calculates all of df immediately, in order to pick values for divisions. This is helpful, but also makes it very easy to trigger a large computation, and the eagerness is inconsistent with Dask's lazy API.
Downsides of this are illustrated in this blog post by @DahnJ. This default behavior makes it very easy for you to do something that's very not performant, and relatively hard to figure out why it's happening.
If you're running a whole script (versus a notebook with separate cells), it would not be immediately clear that set_index is even calling compute internally, and you'll be left puzzling over the dashboard about why the "calculation seems to re-set and begin again".
Additionally, since it's so easy to compute divisions automatically, users rarely interact with them. This makes the idea of setting them manually intimidating, when in fact it's actually pretty straightforward. (Hopefully better documentation helps with this #8379). And since users don't interact with them, they wouldn't realize there's even an option to avoid this computation until they read the docstring of set_index.
Basically, I think not passing divisions to set_index should be considered an antipattern. There are only a few cases where it's unavoidable and you need to calculate divisions, and it's always far less performant.
Therefore, if we agree it's an antipattern, we should have our API guide users away from it (the "pit of success").
So I propose:
>>> df.set_index('foo')
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
...
ValueError: No divisions specified. Pass `divisions=[...]` to specify the "dividing lines" used to split
the new index into partitions.
Alternatively, pass `divisions="compute"` to have Dask compute good divisions for you right now.
This can be expensive, since it computes the entire DataFrame eagerly.
If you use `divisions="compute"`, we recommend printing the `.divisions` of the resulting DataFrame
and copy-pasting them back into this line of code, so if you re-run it, divisions won't be re-calculated.
For more information, see https://docs.dask.org/en/stable/generated/dask.dataframe.DataFrame.set_index.html.This way:
- Users have to be explicitly aware of the eager compute, and have to ask for it.
- Users can't pick the antipattern without first being presented with the recommended alternative.
- The
"compute"in the code is hopefully a flag to other readers that some computation is going on; you don't have to knowset_index's default behavior to recognize this.
Of course, we'd have a deprecation cycle with a warning before this became an error. I'd also be excited to add some tools to help users:
- Generate reasonable divisions when they know something about the data (save typing)
- Check whether the divisions they've picked are good
- Suggest "how" to pick divisions?
Activity
MrPowers commentedon Dec 6, 2021
@gjoseph92 - Rather than "If you use
divisions="compute", we recommend printing the.divisionsof the resulting DataFrame and copy-pasting them back into this line of code, so if you re-run it, divisions won't be re-calculated.", can we just include the divisions in the error message. Or perhaps we can expose another API? Making users setdivisions="compute", then calling.divisions, then copy pasting them in while removingdivisions="compute"seems a bit tedious. Thoughts?gjoseph92 commentedon Dec 6, 2021
I'd also like a better API here. Unfortunately though, the whole point is that we don't know the divisions yet, and to figure them out we have to do a big compute. (So we can't include them in this error message because we don't know them yet.) I think it's always going to be three steps:
set_index(), see the error messageset_index(divisions="compute"), ordf.recommended_divisions().compute()or something)set_indexcall.MrPowers commentedon Dec 6, 2021
I like
df.recommended_divisions().compute()🤓That way users that are familiar with the process can easily skip step #1.
It seems like manually setting the divisions will run into "notebook scaling issues" at some point. What do we recommend if the recommended divisions array contains 100,000 elements?
gjoseph92 commentedon Dec 6, 2021
I don't know, I've also been wondering about this. My vague idea of tools to "generate reasonable divisions when they know something about the data (save typing)" are hoping to help with this, but it certainly won't cover a lot of cases. At some point, you might just have 100,000 hardcoded divisions you need to use. At that point, I don't know. Write it to a file? Put it in a cell and collapse it? It's awkward for sure. I just think the pain of dealing with a hardcoded 100,000-element list is still a lot better than a 5-minute recompute of that list every time you run the notebook.
jsignell commentedon Dec 23, 2021
I really like the idea of separating out the steps that are all wrapped up into
set_index. I agree that it currently does too much, and is both surprising and opaque. I took a crack at implementing acalculate_divisionsmethod in #8517 and I really liked how the new approach felt.In general in Dask we tend to avoid having the user interact directly with the daskiness of dataframes (partitions, divisions, meta...). I imagine that was the motivation for putting so much in
set_indexrather than splitting it up. I worry that by hiding that stuff too much we end up kind of overloading certain methods. So you end up recommending things like in #8513 where the error is that the divisions are unknown, and the solution is to callset_index(even though the user doesn't care about the index). In my mindset_indexis a kind of special method because I know it has the side effect of setting division, but that is not particularly obvious to the casual user.I guess I think that we should encourage dask-specific methods that let people manipulate partitions, divisions, meta... we can still have the magic on set_index, but not at the expense of having less complicated methods.
gjoseph92 commentedon Jan 5, 2022
@jsignell totally agreed. If we can't fully hide complexity, it's better not to obscure it at all. For better or worse, I think divisions are the sort of thing users do actually have to understand and interact with in order to use dask effectively.
I'm liking how your new approach feels too!
scharlottej13 commentedon Mar 2, 2022
@gjoseph92 did you and @jsignell still want to keep this discussion open?
gjoseph92 commentedon Mar 3, 2022
Sure, seems like we agreed that we should do this, but maybe we'll just leave the issue open until it actually happens?