MCPcopy Create free account
hub / github.com/dask/dask / map_partitions

Method map_partitions

dask/dataframe/dask_expr/_collection.py:991–1133  ·  view source on GitHub ↗

Apply a Python function to each partition Parameters ---------- func : function Function applied to each partition. args, kwargs : Arguments and keywords to pass to the function. Arguments and keywords may contain ``FrameBase`` or

(
        self,
        func,
        *args,
        meta=no_default,
        enforce_metadata=True,
        transform_divisions=True,
        clear_divisions=False,
        align_dataframes=False,
        parent_meta=None,
        required_columns=None,
        **kwargs,
    )

Source from the content-addressed store, hash-verified

989
990 @insert_meta_param_description(pad=12)
991 def map_partitions(
992 self,
993 func,
994 *args,
995 meta=no_default,
996 enforce_metadata=True,
997 transform_divisions=True,
998 clear_divisions=False,
999 align_dataframes=False,
1000 parent_meta=None,
1001 required_columns=None,
1002 **kwargs,
1003 ):
1004 """Apply a Python function to each partition
1005
1006 Parameters
1007 ----------
1008 func : function
1009 Function applied to each partition.
1010 args, kwargs :
1011 Arguments and keywords to pass to the function. Arguments and
1012 keywords may contain ``FrameBase`` or regular python objects.
1013 DataFrame-like args (both dask and pandas) must have the same
1014 number of partitions as ``self`` or comprise a single partition.
1015 Key-word arguments, Single-partition arguments, and general
1016 python-object arguments will be broadcasted to all partitions.
1017 enforce_metadata : bool, default True
1018 Whether to enforce at runtime that the structure of the DataFrame
1019 produced by ``func`` actually matches the structure of ``meta``.
1020 This will rename and reorder columns for each partition, and will
1021 raise an error if this doesn't work, but it won't raise if dtypes
1022 don't match.
1023 transform_divisions : bool, default True
1024 Whether to apply the function onto the divisions and apply those
1025 transformed divisions to the output.
1026 clear_divisions : bool, default False
1027 Whether divisions should be cleared. If True, `transform_divisions`
1028 will be ignored.
1029 required_columns : list or None, default None
1030 List of columns that ``func`` requires for execution. These columns
1031 must belong to the first DataFrame argument (in ``args``). If None
1032 is specified (the default), the query optimizer will assume that
1033 all input columns are required.
1034 $META
1035
1036 Examples
1037 --------
1038 Given a DataFrame, Series, or Index, such as:
1039
1040 >>> import pandas as pd
1041 >>> import dask.dataframe as dd
1042 >>> df = pd.DataFrame({'x': [1, 2, 3, 4, 5],
1043 ... 'y': [1., 2., 3., 4., 5.]})
1044 >>> ddf = dd.from_pandas(df, npartitions=2)
1045
1046 One can use ``map_partitions`` to apply a function on each partition.
1047 Extra arguments and keywords can optionally be provided, and will be
1048 passed to the function after the partition.

Callers 15

to_dask_arrayMethod · 0.95
valuesMethod · 0.95
sumMethod · 0.95
prodMethod · 0.95
skewMethod · 0.95
semMethod · 0.95
meanMethod · 0.95
maxMethod · 0.95
anyMethod · 0.95
allMethod · 0.95
idxminMethod · 0.95
idxmaxMethod · 0.95

Calls 1

map_partitionsFunction · 0.70

Tested by 15

test_from_delayed_daskFunction · 0.36
test_from_delayed_fusionFunction · 0.36
test_disk_shuffleFunction · 0.36
test_task_shuffleFunction · 0.36
test_task_shuffle_indexFunction · 0.36
test_merge_empty_left_dfFunction · 0.36
test_map_partitionsFunction · 0.36