Skip to content

[Discussion] Don't compute divisions by default in set_index? #8435

Open

Description

@gjoseph92

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:

  1. Users have to be explicitly aware of the eager compute, and have to ask for it.
  2. Users can't pick the antipattern without first being presented with the recommended alternative.
  3. The "compute" in the code is hopefully a flag to other readers that some computation is going on; you don't have to know set_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:

  1. Generate reasonable divisions when they know something about the data (save typing)
  2. Check whether the divisions they've picked are good
  3. Suggest "how" to pick divisions?

cc @jsignell @scharlottej13 @ian-r-rose @jrbourbeau @jcrist

Activity

  1. MrPowers commented on Dec 6, 2021

    @MrPowers
    Contributor

    @gjoseph92 - Rather than "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.", can we just include the divisions in the error message. Or perhaps we can expose another API? Making users set divisions="compute", then calling .divisions, then copy pasting them in while removing divisions="compute" seems a bit tedious. Thoughts?

  2. gjoseph92 commented on Dec 6, 2021

    @gjoseph92
    CollaboratorAuthor

    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:

    1. Call set_index(), see the error message
    2. Compute the divisions in some way (set_index(divisions="compute"), or df.recommended_divisions().compute() or something)
    3. Copy-paste the recommended divisions into the original set_index call.
  3. MrPowers commented on Dec 6, 2021

    @MrPowers
    Contributor

    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?

  4. gjoseph92 commented on Dec 6, 2021

    @gjoseph92
    CollaboratorAuthor

    What do we recommend if the recommended divisions array contains 100,000 elements?

    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.

  5. jsignell commented on Dec 23, 2021

    @jsignell
    Member

    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 a calculate_divisions method 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_index rather 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 call set_index (even though the user doesn't care about the index). In my mind set_index is 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.

  6. gjoseph92 commented on Jan 5, 2022

    @gjoseph92
    CollaboratorAuthor

    @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!

  7. scharlottej13 commented on Mar 2, 2022

    @scharlottej13
    Contributor

    @gjoseph92 did you and @jsignell still want to keep this discussion open?

  8. added
    discussion
    on Mar 2, 2022
  9. gjoseph92 commented on Mar 3, 2022

    @gjoseph92
    CollaboratorAuthor

    Sure, seems like we agreed that we should do this, but maybe we'll just leave the issue open until it actually happens?

  10. added
    needs attention
    on Aug 26, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    dataframediscussionneeds attention

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions