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

Method map_partitions

dask/dataframe/dask_expr/_collection.py:1027–1169  ·  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

1025
1026 @insert_meta_param_description(pad=12)
1027 def map_partitions(
1028 self,
1029 func,
1030 *args,
1031 meta=no_default,
1032 enforce_metadata=True,
1033 transform_divisions=True,
1034 clear_divisions=False,
1035 align_dataframes=False,
1036 parent_meta=None,
1037 required_columns=None,
1038 **kwargs,
1039 ):
1040 """Apply a Python function to each partition
1041
1042 Parameters
1043 ----------
1044 func : function
1045 Function applied to each partition.
1046 args, kwargs :
1047 Arguments and keywords to pass to the function. Arguments and
1048 keywords may contain ``FrameBase`` or regular python objects.
1049 DataFrame-like args (both dask and pandas) must have the same
1050 number of partitions as ``self`` or comprise a single partition.
1051 Key-word arguments, Single-partition arguments, and general
1052 python-object arguments will be broadcasted to all partitions.
1053 enforce_metadata : bool, default True
1054 Whether to enforce at runtime that the structure of the DataFrame
1055 produced by ``func`` actually matches the structure of ``meta``.
1056 This will rename and reorder columns for each partition, and will
1057 raise an error if this doesn't work, but it won't raise if dtypes
1058 don't match.
1059 transform_divisions : bool, default True
1060 Whether to apply the function onto the divisions and apply those
1061 transformed divisions to the output.
1062 clear_divisions : bool, default False
1063 Whether divisions should be cleared. If True, `transform_divisions`
1064 will be ignored.
1065 required_columns : list or None, default None
1066 List of columns that ``func`` requires for execution. These columns
1067 must belong to the first DataFrame argument (in ``args``). If None
1068 is specified (the default), the query optimizer will assume that
1069 all input columns are required.
1070 $META
1071
1072 Examples
1073 --------
1074 Given a DataFrame, Series, or Index, such as:
1075
1076 >>> import pandas as pd
1077 >>> import dask.dataframe as dd
1078 >>> df = pd.DataFrame({'x': [1, 2, 3, 4, 5],
1079 ... 'y': [1., 2., 3., 4., 5.]})
1080 >>> ddf = dd.from_pandas(df, npartitions=2)
1081
1082 One can use ``map_partitions`` to apply a function on each partition.
1083 Extra arguments and keywords can optionally be provided, and will be
1084 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