pandas-dev/pandas · error · ValueError

by_row= not allowed

Error message

by_row={by_row} not allowed

What it means

Raised in the `FrameApply` (DataFrame apply) constructor when `by_row` is set to any value other than `False` or the literal `'compat'`. `by_row` is a Series.apply-specific parameter that pandas forwards internally; on DataFrame.apply only the two sentinel values are meaningful, so anything else (including `True`) is rejected up front.

Solutions

  1. Remove the `by_row` argument from DataFrame.apply calls — it is not a public knob for frames.
  2. If you need row-wise application, use `df.apply(func, axis=1)`.
  3. If you truly need the compat behavior, pass the literal string `by_row='compat'`.

Example fix

// before
df.apply(func, by_row=True)
// after
df.apply(func, axis=1)
Defensive patterns

Strategy: type-guard

Validate before calling

def frame_apply(df, func, by_row=False, **kw):
    if by_row not in (False, 'compat'):
        raise ValueError("DataFrame.apply only accepts by_row=False or 'compat'; got %r" % (by_row,))
    return df.apply(func, by_row=by_row, **kw)

Type guard

from typing import Literal
ByRow = Literal[False, 'compat']
def is_valid_frame_by_row(by_row) -> bool:
    return by_row is False or by_row == 'compat'

Try / catch

try:
    out = df.apply(func, by_row=by_row)
except ValueError as e:
    if 'by_row=' in str(e):
        out = df.apply(func, axis=1)  # use the proper row-wise path
    else:
        raise

Prevention

When it happens

Trigger: Calling `df.apply(func, by_row=True)`, `df.apply(func, by_row='rows')`, or any non-sentinel value. The check `by_row is not False and by_row != 'compat'` is strict.

Common situations: Users copy a `by_row=...` argument from a Series.apply example into a DataFrame.apply call, or pass `True` assuming it is a boolean toggle.

Related errors


AI-assisted analysis of pandas-dev/pandas@3b7651241d (2026-08-11). Data as JSON: /api/errors/adf08763ac53470d. Report an issue: GitHub.

Appendix: source

Thrown at pandas/core/apply.py:932

class FrameApply(NDFrameApply):
    obj: DataFrame

    def __init__(
        self,
        obj: AggObjType,
        func: AggFuncType,
        raw: bool,
        result_type: str | None,
        *,
        by_row: Literal[False, "compat"] = False,
        engine: str = "python",
        engine_kwargs: dict[str, bool] | None = None,
        args,
        kwargs,
    ) -> None:
        if by_row is not False and by_row != "compat":
            raise ValueError(f"by_row={by_row} not allowed")
        super().__init__(
            obj,
            func,
            raw,
            result_type,
            by_row=by_row,
            engine=engine,
            engine_kwargs=engine_kwargs,
            args=args,
            kwargs=kwargs,
        )

    # ---------------------------------------------------------------
    # Abstract Methods

    @property
    @abc.abstractmethod
    def result_index(self) -> Index:

View on GitHub (pinned to 3b7651241d)