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