maxframe.liteframe.LiteFrame.apply#

LiteFrame.apply(func, axis=0, *, result_type=None, dtypes=None, skip_infer=False, input_frame_type='pandas', args=(), **kwargs)#

Apply func per column (axis=0) or per row (axis=1).

Mirrors pandas.DataFrame.apply:

  • axis=0: func receives each column as a Series and may return a scalar, Series, or DataFrame. All row chunks are concatenated first so the UDF sees the whole column.

  • axis=1: func receives each row as a Series and may return a scalar, Series, or dict. Results are combined one output row per input row. Because per-row UDFs are not mock-inferable from the whole frame, axis=1 generally requires explicit dtypes or a return type annotation.

Parameters:
  • func (callable or callable class) –

    Function taking a Series (a column for axis=0, a row for axis=1) and returning a scalar, Series, dict, or DataFrame.

    If func is a class, it is instantiated once per chunk, __call__ is invoked per column (axis=0) or per row (axis=1), and close() (if defined) is always called at the end of the chunk — even when __call__ receives single rows/columns.

  • axis ({0, 1}, default 0) – axis=0 iterates over columns; axis=1 iterates over rows.

  • result_type ({"expand", "reduce", "broadcast", None}, default None) –

    Only applies when axis=1. Controls how list-like results (dict, list, Series) are combined into the output frame:

    • None (default): Series results are auto-expanded to columns (matching pandas, where a per-row Series is combined into a multi-column frame). dict and list results are kept as a single object column (one cell per row) — pandas 2.3.3 does not expand dict keys or lists unless result_type='expand' is given; declaring multiple columns for a dict / list result raises.

    • "expand": dict / list / Series results are always expanded to columns (dict keys / positional indices / Series index become column names).

    • "reduce": dict / list results are kept as a single column of objects (one cell per row), matching pandas result_type='reduce'. Series results are still expanded to columns (pandas behavior).

    • "broadcast": results are broadcast to the original input shape — the output has the same column names as the input. Requires list-like results whose length matches the input column count.

  • dtypes (dict or pd.Series, optional) – Expected output dtypes. If omitted, inferred from func’s return annotation or a mock call.

  • skip_infer (bool, default False) – If True, skip dtype inference and produce an untyped output.

  • 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 whose dtypes match the (inferred or explicit) output schema.

Return type:

LiteFrame

See also

apply_chunk

Apply a function per execution chunk.

transform

Transform per column or per row.

map

Apply a function element-wise.

Examples

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

Apply a reduction per column (axis=0, default) — func receives each column as a Series:

>>> result = lf.apply(lambda col: col.sum())
>>> result.execute()
    a   b
0   6  60

Apply per row (axis=1) — func receives each row as a Series. Declare the output schema with dtypes:

>>> result = lf.apply(lambda row: {'sum': row['a'] + row['b']},
...                   axis=1, dtypes={'sum': int})
>>> result.execute()
   sum
0   11
1   22
2   33

With axis=1 and result_type='expand', dict results are expanded to columns:

>>> result = lf.apply(lambda row: {'a2': row['a'] ** 2, 'b2': row['b'] ** 2},
...                   axis=1, result_type='expand',
...                   dtypes={'a2': int, 'b2': int})
>>> result.execute()
   a2    b2
0   1   100
1   4   400
2   9   900