Skip to content

map_partitions only aligns arguments in *args; **kwargs are concatenated first #8308

Open

Description

@gjoseph92

When passing an auxiliary dask DataFrame to map_partitions, its chunks are aligned to the main DataFrame and the function receives one chunk of each per task. If you give the same input as a kwarg, the function receives the entire DataFrame concatenated into one.

It's somewhat unclear from the docstring if this is expected behavior:

        Arguments and keywords to pass to the function.  At least one of the
        args should be a Dask.dataframe. Arguments and keywords may contain
        ``Scalar``, ``Delayed`` or regular python objects. DataFrame-like args
        (both dask and pandas) will be repartitioned to align (if necessary)
        before applying the function.

but either way it's a bit unintuitive that using a positional vs a kwarg would change behavior like this.

Minimal Complete Verifiable Example:

import dask
df = dask.datasets.timeseries()
df2 = dask.datasets.timeseries()

def x(p, other):
    assert len(p) == len(other), f"{len(p)=}, {len(other)=}"
    return p.x - other.x

# Works, because `df2` given positionally
df.map_partitions(x, df2).compute()

# Fails, because `df2` given as kwarg
df.map_partitions(x, other=df2).compute()
# AssertionError: len(p)=86400, len(other)=2592000

The problem is likely that map_partitions finalizes (via unpack_collections) everything in kwargs. Additionally, partitionwise_graph has lots of unpacking logic for args to format them into proper blockwise (name, indices) pairs, but kwargs are passed through straight to blockwise to be treated as 0D objects.

Anything else we need to know?:

I would be okay keeping this behavior, but it should be documented much more clearly. Possibly even should raise a warning if a multi-chunk DataFrame/Array is given as a kwarg.

cc @rjzamora @ian-r-rose

Environment:

  • Dask version: 824244c
  • Python version: 3.9.1
  • Operating System: macOS
  • Install method (conda, pip, source): source

Activity

  1. ian-r-rose commented on Oct 27, 2021

    @ian-r-rose
    Collaborator

    I basically agree with your diagnosis and solution @gjoseph92. It's an unfortunate gotcha that can be fixed with better docs and an appropriate warning.

  2. added
    documentation
    good first issue
    on Oct 28, 2021
  3. removed
    good first issue
    on Oct 28, 2021
  4. jsignell commented on Nov 8, 2021

    @jsignell
    Member

    Does the align_dataframes kwarg resolve this issue, or is there still work to do here in your opinion @gjoseph92?

  5. gjoseph92 commented on Nov 8, 2021

    @gjoseph92
    CollaboratorAuthor

    Unfortunately I think the docs still don't make it explicit enough that kwargs don't get aligned (whether align_dataframes is True or False). I should have thought to mention that in #6628, sorry.

    A docs update to make the current state clearer would be good, though I still think we should actually change the current state since it's pretty weird.

  6. ian-r-rose commented on Jun 24, 2022

    @ian-r-rose
    Collaborator

    This wound up being the cause of a fairly tricky-to-debug issue in dask-geopandas. I think it would be a good idea to raise a warning if DataFrame/Series are passed in via kwargs, as @gjoseph92 suggests above.

  7. added
    good second issue
    on Jun 24, 2022
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

    dataframedocumentationgood second issue

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions