def apply(self, func, convert_dtype=True, args=(), **kwds):
if len(self) == <NUM_LIT:0>:<EOL><INDENT>return self._constructor(dtype=self.dtype,<EOL>index=self.index).__finalize__(self)<EOL><DEDENT>if isinstance(func, (list, dict)):<EOL><INDENT>return self.aggregate(func, *args, **kwds)<EOL><DEDENT>if isinstance(func, str):<EOL><INDENT>return self._try_aggregate_string_function(func, *args, **kwds)<EOL><DEDENT>if kwds or args and not isinstance(func, np.ufunc):<EOL><INDENT>def f(x):<EOL><INDENT>return func(x, *args, **kwds)<EOL><DEDENT><DEDENT>else:<EOL><INDENT>f = func<EOL><DEDENT>with np.errstate(all='<STR_LIT:ignore>'):<EOL><INDENT>if isinstance(f, np.ufunc):<EOL><INDENT>return f(self)<EOL><DEDENT>if is_extension_type(self.dtype):<EOL><INDENT>mapped = self._values.map(f)<EOL><DEDENT>else:<EOL><INDENT>values = self.astype(object).values<EOL>mapped = lib.map_infer(values, f, convert=convert_dtype)<EOL><DEDENT><DEDENT>if len(mapped) and isinstance(mapped[<NUM_LIT:0>], Series):<EOL><INDENT>from pandas.core.frame import DataFrame<EOL>return DataFrame(mapped.tolist(), index=self.index)<EOL><DEDENT>else:<EOL><INDENT>return self._constructor(mapped,<EOL>index=self.index).__finalize__(self)<EOL><DEDENT>
Invoke function on values of Series.

Can be ufunc (a NumPy function that applies to the entire Series)
or a Python function that only works on single values.

Parameters
----------
func : function
    Python function or NumPy ufunc to apply.
convert_dtype : bool, default True
    Try to find better dtype for elementwise function results. If
    False, leave as dtype=object.
args : tuple
    Positional arguments passed to func after the series value.
**kwds
    Additional keyword arguments passed to func.

Returns
-------
Series or DataFrame
    If func returns a Series object the result will be a DataFrame.

See Also
--------
Series.map: For element-wise operations.
Series.agg: Only perform aggregating type operations.
Series.transform: Only perform transforming type operations.

Examples
--------
Create a series with typical summer temperatures for each city.

>>> s = pd.Series([20, 21, 12],
...               index=['London', 'New York', 'Helsinki'])
>>> s
London      20
New York    21
Helsinki    12
dtype: int64

Square the values by defining a function and passing it as an
argument to ``apply()``.

>>> def square(x):
...     return x ** 2
>>> s.apply(square)
London      400
New York    441
Helsinki    144
dtype: int64

Square the values by passing an anonymous function as an
argument to ``apply()``.

>>> s.apply(lambda x: x ** 2)
London      400
New York    441
Helsinki    144
dtype: int64

Define a custom function that needs additional positional
arguments and pass these additional arguments using the
``args`` keyword.

>>> def subtract_custom_value(x, custom_value):
...     return x - custom_value

>>> s.apply(subtract_custom_value, args=(5,))
London      15
New York    16
Helsinki     7
dtype: int64

Define a custom function that takes keyword arguments
and pass these arguments to ``apply``.

>>> def add_custom_values(x, **kwargs):
...     for month in kwargs:
...         x += kwargs[month]
...     return x

>>> s.apply(add_custom_values, june=30, july=20, august=25)
London      95
New York    96
Helsinki    87
dtype: int64

Use a function from the Numpy library.

>>> s.apply(np.log)
London      2.995732
New York    3.044522
Helsinki    2.484907
dtype: float64