pandas-dev/pandas · error · ValueError

invalid value for result_type, must be one of

Error message

invalid value for result_type, must be one of {None, 'reduce', 'broadcast', 'expand'}

What it means

DataFrame.apply() accepts a result_type parameter that controls how row-wise (axis=1) results are assembled: None (infer), 'reduce' (return Series), 'broadcast' (return DataFrame with original index), or 'expand' (expand list-like results into columns). Any other string or value raises a ValueError. This validation happens in the Apply constructor before any computation begins.

Solutions

  1. Use one of the exact valid values: None, 'reduce', 'broadcast', or 'expand'.
  2. Validate before calling: assert result_type in (None, 'reduce', 'broadcast', 'expand').
  3. Omit result_type entirely if the default inference behavior is acceptable.

Example fix

# before
df.apply(func, axis=1, result_type='expanded')

# after
df.apply(func, axis=1, result_type='expand')
Defensive patterns

Strategy: validation

Validate before calling

VALID_RESULT_TYPES = (None, 'reduce', 'broadcast', 'expand')

def safe_apply(df, func, axis=0, result_type=None, **kwargs):
    if result_type not in VALID_RESULT_TYPES:
        raise ValueError(f"result_type must be one of {VALID_RESULT_TYPES}, got {result_type!r}")
    return df.apply(func, axis=axis, result_type=result_type, **kwargs)

Type guard

def is_valid_result_type(rt) -> bool:
    return rt in (None, 'reduce', 'broadcast', 'expand')

Try / catch

try:
    result = df.apply(func, axis=1, result_type=rt)
except ValueError as e:
    if "result_type" in str(e):
        result = df.apply(func, axis=1)  # default inference
    else:
        raise

Prevention

When it happens

Trigger: Calling df.apply(func, axis=1, result_type='expand') with a typo like 'expanded' or 'broadcasting'. Passing result_type from a variable that was set to an invalid string. Using True/False or 0/1 instead of the string sentinels.

Common situations: Typos in the result_type string — the valid values are short and easily misspelled. Passing result_type from configuration without validation. Copying code from documentation and introducing a typo.

Related errors


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

Appendix: source

Thrown at pandas/core/apply.py:293

        engine: str = "python",
        engine_kwargs: dict[str, bool] | None = None,
        args,
        kwargs,
    ) -> None:
        self.obj = obj
        self.raw = raw

        assert by_row is False or by_row in ["compat", "_compat"]
        self.by_row = by_row

        self.args = args or ()
        self.kwargs = kwargs or {}

        self.engine = engine
        self.engine_kwargs = {} if engine_kwargs is None else engine_kwargs

        if result_type not in [None, "reduce", "broadcast", "expand"]:
            raise ValueError(
                "invalid value for result_type, must be one "
                "of {None, 'reduce', 'broadcast', 'expand'}"
            )

        self.result_type = result_type

        self.func = func

    @abc.abstractmethod
    def apply(self) -> DataFrame | Series:
        pass

    @abc.abstractmethod
    def agg_or_apply_list_like(
        self, op_name: Literal["agg", "apply"]
    ) -> DataFrame | Series:
        pass

View on GitHub (pinned to 3b7651241d)