maxframe.liteframe.groupby.LiteFrameGroupBy.apply#

LiteFrameGroupBy.apply(func, *, dtypes=None, skip_infer=False, input_frame_type='pandas', args=(), **kwargs)#

Apply func to each group as a whole frame.

The user function receives one DataFrame per group (not per column or row) and may return a DataFrame, Series, or scalar.

apply may change the row count / row order, so hidden columns are not propagated – they only survive if the UDF explicitly returns them.

Parameters:
  • func (callable or callable class) –

    Function taking a group DataFrame and returning a DataFrame, Series, or scalar.

    If func is a class, it is instantiated once per group, __call__(batch, end=True) is invoked once with end=True (each call is a complete invocation that must flush its output), and close() (if defined) is always called at the end of each group.

  • dtypes (dict or pd.Series, optional) – Expected output dtypes.

  • skip_infer (bool, default False) – If True, skip dtype inference.

  • input_frame_type ({"pandas", "polars"}, default "pandas") – Frame type passed to func.

  • args (tuple) – Extra positional arguments forwarded to func.

  • **kwargs – Extra keyword arguments forwarded to func.

Returns:

A new LiteFrame with the results concatenated across groups.

Return type:

LiteFrame

See also

groupby_transform

Per-group column transform.

groupby_apply_chunk

Per-group-chunk application.

Examples

>>> import maxframe.liteframe as mlf
>>> lf = mlf.LiteFrame({'key': ['a', 'a', 'b', 'b'], 'val': [1, 2, 3, 4]})

Return one row per group (reduction):

>>> result = lf.groupby('key').apply(lambda g: g['val'].sum(),
...                                 dtypes={'val': int})
>>> result.execute()
  key  val
0   a    3
1   b    7

Return all rows of each group (identity):

>>> result = lf.groupby('key')[['key', 'val']].apply(
...     lambda g: g, dtypes={'key': str, 'val': int})
>>> result.execute()
  key  val
0   a    1
1   a    2
2   b    3
3   b    4