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)
| 4397 | |
| 4398 | @insert_meta_param_description(pad=12) |
| 4399 | def apply(self, function, *args, meta=no_default, axis=0, **kwargs): |
| 4400 | """Parallel version of pandas.Series.apply |
| 4401 | |
| 4402 | Parameters |
| 4403 | ---------- |
| 4404 | func : function |
| 4405 | Function to apply |
| 4406 | $META |
| 4407 | args : tuple |
| 4408 | Positional arguments to pass to function in addition to the value. |
| 4409 | |
| 4410 | Additional keyword arguments will be passed as keywords to the function. |
| 4411 | |
| 4412 | Returns |
| 4413 | ------- |
| 4414 | applied : Series or DataFrame if func returns a Series. |
| 4415 | |
| 4416 | Examples |
| 4417 | -------- |
| 4418 | >>> import dask.dataframe as dd |
| 4419 | >>> s = pd.Series(range(5), name='x') |
| 4420 | >>> ds = dd.from_pandas(s, npartitions=2) |
| 4421 | |
| 4422 | Apply a function elementwise across the Series, passing in extra |
| 4423 | arguments in ``args`` and ``kwargs``: |
| 4424 | |
| 4425 | >>> def myadd(x, a, b=1): |
| 4426 | ... return x + a + b |
| 4427 | >>> res = ds.apply(myadd, args=(2,), b=1.5) # doctest: +SKIP |
| 4428 | |
| 4429 | By default, dask tries to infer the output metadata by running your |
| 4430 | provided function on some fake data. This works well in many cases, but |
| 4431 | can sometimes be expensive, or even fail. To avoid this, you can |
| 4432 | manually specify the output metadata with the ``meta`` keyword. This |
| 4433 | can be specified in many forms, for more information see |
| 4434 | ``dask.dataframe.utils.make_meta``. |
| 4435 | |
| 4436 | Here we specify the output is a Series with name ``'x'``, and dtype |
| 4437 | ``float64``: |
| 4438 | |
| 4439 | >>> res = ds.apply(myadd, args=(2,), b=1.5, meta=('x', 'f8')) |
| 4440 | |
| 4441 | In the case where the metadata doesn't change, you can also pass in |
| 4442 | the object itself directly: |
| 4443 | |
| 4444 | >>> res = ds.apply(lambda x: x + 1, meta=ds) |
| 4445 | |
| 4446 | See Also |
| 4447 | -------- |
| 4448 | Series.map_partitions |
| 4449 | """ |
| 4450 | self._validate_axis(axis) |
| 4451 | if meta is no_default: |
| 4452 | meta = expr.emulate(M.apply, self, function, args=args, udf=True, **kwargs) |
| 4453 | warnings.warn(meta_warning(meta, method="apply")) |
| 4454 | return new_collection(self.expr.apply(function, *args, meta=meta, **kwargs)) |
| 4455 | |
| 4456 | @classmethod |
no test coverage detected