Parallel version of pandas.Series.apply Parameters ---------- func : function Function to apply $META args : tuple Positional arguments to pass to function in addition to the value. Additional keyword arguments will be passed
(self, function, *args, meta=no_default, axis=0, **kwargs)
| 4343 | |
| 4344 | @insert_meta_param_description(pad=12) |
| 4345 | def apply(self, function, *args, meta=no_default, axis=0, **kwargs): |
| 4346 | """Parallel version of pandas.Series.apply |
| 4347 | |
| 4348 | Parameters |
| 4349 | ---------- |
| 4350 | func : function |
| 4351 | Function to apply |
| 4352 | $META |
| 4353 | args : tuple |
| 4354 | Positional arguments to pass to function in addition to the value. |
| 4355 | |
| 4356 | Additional keyword arguments will be passed as keywords to the function. |
| 4357 | |
| 4358 | Returns |
| 4359 | ------- |
| 4360 | applied : Series or DataFrame if func returns a Series. |
| 4361 | |
| 4362 | Examples |
| 4363 | -------- |
| 4364 | >>> import dask.dataframe as dd |
| 4365 | >>> s = pd.Series(range(5), name='x') |
| 4366 | >>> ds = dd.from_pandas(s, npartitions=2) |
| 4367 | |
| 4368 | Apply a function elementwise across the Series, passing in extra |
| 4369 | arguments in ``args`` and ``kwargs``: |
| 4370 | |
| 4371 | >>> def myadd(x, a, b=1): |
| 4372 | ... return x + a + b |
| 4373 | >>> res = ds.apply(myadd, args=(2,), b=1.5) # doctest: +SKIP |
| 4374 | |
| 4375 | By default, dask tries to infer the output metadata by running your |
| 4376 | provided function on some fake data. This works well in many cases, but |
| 4377 | can sometimes be expensive, or even fail. To avoid this, you can |
| 4378 | manually specify the output metadata with the ``meta`` keyword. This |
| 4379 | can be specified in many forms, for more information see |
| 4380 | ``dask.dataframe.utils.make_meta``. |
| 4381 | |
| 4382 | Here we specify the output is a Series with name ``'x'``, and dtype |
| 4383 | ``float64``: |
| 4384 | |
| 4385 | >>> res = ds.apply(myadd, args=(2,), b=1.5, meta=('x', 'f8')) |
| 4386 | |
| 4387 | In the case where the metadata doesn't change, you can also pass in |
| 4388 | the object itself directly: |
| 4389 | |
| 4390 | >>> res = ds.apply(lambda x: x + 1, meta=ds) |
| 4391 | |
| 4392 | See Also |
| 4393 | -------- |
| 4394 | Series.map_partitions |
| 4395 | """ |
| 4396 | self._validate_axis(axis) |
| 4397 | if meta is no_default: |
| 4398 | meta = expr.emulate(M.apply, self, function, args=args, udf=True, **kwargs) |
| 4399 | warnings.warn(meta_warning(meta)) |
| 4400 | return new_collection(self.expr.apply(function, *args, meta=meta, **kwargs)) |
| 4401 | |
| 4402 | @classmethod |
no test coverage detected