You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
map_partitions only aligns arguments in *args; **kwargs are concatenated first #8308
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:
importdaskdf=dask.datasets.timeseries()
df2=dask.datasets.timeseries()
defx(p, other):
assertlen(p) ==len(other), f"{len(p)=}, {len(other)=}"returnp.x-other.x# Works, because `df2` given positionallydf.map_partitions(x, df2).compute()
# Fails, because `df2` given as kwargdf.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.
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.
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.
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.
Activity
ian-r-rose commentedon Oct 27, 2021
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.
jsignell commentedon Nov 8, 2021
Does the
align_dataframeskwarg resolve this issue, or is there still work to do here in your opinion @gjoseph92?gjoseph92 commentedon Nov 8, 2021
Unfortunately I think the docs still don't make it explicit enough that kwargs don't get aligned (whether
align_dataframesis 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.
Workaround for the fact that we can't include a partitioned _Frame in
ian-r-rose commentedon Jun 24, 2022
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.
Don't put GeoSeries in `map_partitions` kwarg (#205)