Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 40 additions & 7 deletions dask/dataframe/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -627,8 +627,25 @@ def map_partitions(self, func, *args, **kwargs):
be the first argument, and these will be passed *after*. Arguments
and keywords may contain ``Scalar``, ``Delayed``, ``partition_info``
or regular python objects. DataFrame-like args (both dask and
pandas) will be repartitioned to align (if necessary) before
applying the function.
pandas) will be repartitioned to align (if necessary) before
applying the function (see ``align_dataframes`` to control).
enforce_metadata : bool, default True
Whether to enforce at runtime that the structure of the DataFrame
produced by ``func`` actually matches the structure of ``meta``.
This will rename and reorder columns for each partition,
and will raise an error if this doesn't work or types don't match.
transform_divisions : bool, default True
Whether to apply the function onto the divisions and apply those
transformed divisions to the output.
align_dataframes : bool, default True
Whether to repartition DataFrame- or Series-like args
(both dask and pandas) so their divisions align before applying
the function. This requires all inputs to have known divisions.
Single-partition inputs will be split into multiple partitions.

If False, all inputs must have either the same number of partitions
or a single partition. Single-partition inputs will be broadcast to
every partition of multi-partition inputs.
$META

Examples
Expand Down Expand Up @@ -5794,6 +5811,7 @@ def map_partitions(
meta=no_default,
enforce_metadata=True,
transform_divisions=True,
align_dataframes=True,
**kwargs,
):
"""Apply Python function on each DataFrame partition.
Expand All @@ -5807,11 +5825,24 @@ def map_partitions(
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.
enforce_metadata : bool
Whether or not to enforce the structure of the metadata at runtime.
before applying the function (see ``align_dataframes`` to control).
enforce_metadata : bool, default True
Whether to enforce at runtime that the structure of the DataFrame
produced by ``func`` actually matches the structure of ``meta``.
This will rename and reorder columns for each partition,
and will raise an error if this doesn't work or types don't match.
transform_divisions : bool, default True
Whether to apply the function onto the divisions and apply those
transformed divisions to the output.
align_dataframes : bool, default True
Whether to repartition DataFrame- or Series-like args
(both dask and pandas) so their divisions align before applying
the function. This requires all inputs to have known divisions.
Single-partition inputs will be split into multiple partitions.

If False, all inputs must have either the same number of partitions
or a single partition. Single-partition inputs will be broadcast to
every partition of multi-partition inputs.
$META
"""
name = kwargs.pop("token", None)
Expand All @@ -5830,8 +5861,10 @@ def map_partitions(

from .multi import _maybe_align_partitions

args = _maybe_from_pandas(args)
args = _maybe_align_partitions(args)
if align_dataframes:
args = _maybe_from_pandas(args)
args = _maybe_align_partitions(args)

dfs = [df for df in args if isinstance(df, _Frame)]
meta_index = getattr(make_meta(dfs[0]), "index", None) if dfs else None
if parent_meta is None and dfs:
Expand Down
16 changes: 16 additions & 0 deletions dask/dataframe/tests/test_dataframe.py
Original file line number Diff line number Diff line change
Expand Up @@ -1060,6 +1060,22 @@ def check(df, df_x):
assert_eq(res, ddf)


def test_align_dataframes():
df1 = pd.DataFrame({"A": [1, 2, 3, 3, 2, 3], "B": [1, 2, 3, 4, 5, 6]})
df2 = pd.DataFrame({"A": [3, 1, 2], "C": [1, 2, 3]})

def merge(a, b):
res = pd.merge(a, b, left_on="A", right_on="A", how="left")
return res

expected = merge(df1, df2)

ddf1 = dd.from_pandas(df1, npartitions=2)
actual = ddf1.map_partitions(merge, df2, align_dataframes=False)

assert_eq(actual, expected, check_index=False, check_divisions=False)


def test_drop_duplicates():
res = d.drop_duplicates()
res2 = d.drop_duplicates(split_every=2)
Expand Down