Skip to main content
`Ctrl`+`K`

# pandas.api.typing.Expanding.apply

Expanding.apply(func, raw=False, engine=None, engine_kwargs=None, args=None, kwargs=None)

  Calculate the expanding custom aggregation function.

  Applies a user-defined function over all data points seen so far.

  Parameters:
    func function

      Must produce a single value from an ndarray input if `raw=True` or a single value from a Series if `raw=False`. Can also accept a Numba JIT function with `engine='numba'` specified.

    raw bool, default False

      - `False` : passes each row or column as a Series to the function.

      - `True` : the passed function will receive ndarray objects instead. Pandas-only attributes such as `.iloc` or `.index` are not available on ndarrays and will raise `AttributeError` if used inside `func`.

      If you are just applying a NumPy reduction function this will achieve much better performance.

    engine str, default None

      - `'cython'` : Runs rolling apply through C-extensions from cython.

      - `'numba'` : Runs rolling apply through JIT compiled code from numba. Only available when `raw` is set to `True`.

      - `None` : Defaults to `'cython'` or globally setting `compute.use_numba`

    engine_kwargs dict, default None

      - For `'cython'` engine, there are no accepted `engine_kwargs`

      - For `'numba'` engine, the engine can accept `nogil` and `parallel` dictionary keys. The values must either be `True` or `False`. The default `engine_kwargs` for the `'numba'` engine is `{'nogil': False, 'parallel': False}` and will be applied to both the `func` and the `apply` rolling aggregation.

    args tuple, default None

      Positional arguments to be passed into func.

    kwargs dict, default None

      Keyword arguments to be passed into func.

  Returns:
    Series or DataFrame

      Return type is the same as the original object with `np.float64` dtype.

  See also

  `Series.expanding`

    Calling expanding with Series data.

  `DataFrame.expanding`

    Calling expanding with DataFrames.

  `Series.apply`

    Aggregating apply for Series.

  `DataFrame.apply`

    Aggregating apply for DataFrame.

  Examples

      >>> ser = pd.Series([1, 2, 3, 4], index=["a", "b", "c", "d"])
      >>> ser.expanding().apply(lambda s: s.max() - 2 * s.min())
      a   -1.0
      b    0.0
      c    1.0
      d    2.0
      dtype: float64