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