pandas-dev/pandas · error · ValueError

cannot broadcast result

Error message

cannot broadcast result

What it means

Raised in DataFrame.apply_broadcast (apply.py:1269) when func, run with result_type='broadcast', returns a 1-D array whose length does not equal the number of rows in the target frame (result_compare != len(res)). Broadcasting requires the per-column return to align exactly with target.shape[0] so it can be assigned into result_values[:, i]; a length mismatch means the broadcast cannot be assembled.

Source

Thrown at pandas/core/apply.py:1269

    def apply_broadcast(self, target: DataFrame) -> DataFrame:
        assert callable(self.func)

        result_values = np.empty_like(target.values)

        # axis which we want to compare compliance
        result_compare = target.shape[0]

        for i, col in enumerate(target.columns):
            res = self.func(target[col], *self.args, **self.kwargs)
            ares = np.asarray(res).ndim

            # must be a scalar or 1d
            if ares > 1:
                raise ValueError("too many dims to broadcast")
            if ares == 1:
                # must match return dim
                if result_compare != len(res):
                    raise ValueError("cannot broadcast result")

            result_values[:, i] = res

        # we *always* preserve the original index / columns
        result = self.obj._constructor(
            result_values, index=target.index, columns=target.columns
        )
        return result

    def apply_standard(self):
        if self.engine == "python":
            results, res_index = self.apply_series_generator()
        else:
            results, res_index = self.apply_series_numba()

        # wrap results
        return self.wrap_results(results, res_index)

View on GitHub (pinned to 71959b8cb9)

Solutions

  1. Ensure func returns an array with exactly len(df) elements per column - reindex or pad as needed.
  2. Move length-changing logic (groupby, resample, dropna) out of the broadcast func and into a separate transform step.
  3. If the result truly has a different length, drop result_type='broadcast' and use plain apply or agg with the right shape semantics.

Example fix

// before
df.apply(lambda c: c.dropna(), result_type='broadcast')
// after
df.apply(lambda c: c.fillna(0), result_type='broadcast')
Defensive patterns

Strategy: validation

Validate before calling

import numpy as np
sample = np.asarray(func(df[df.columns[0]], *args, **kwargs))
if sample.ndim == 1 and len(sample) != len(df):
    raise ValueError(f'func returned length {len(sample)} but frame has {len(df)} rows; cannot broadcast')

Type guard

def broadcast_length_matches(func, target_len: int, sample_input) -> bool:
    import numpy as np
    r = np.asarray(func(sample_input))
    return r.ndim == 0 or (r.ndim == 1 and len(r) == target_len)

Try / catch

try:
    df.apply(func, result_type='broadcast')
except ValueError as e:
    if 'cannot broadcast result' in str(e):
        df.apply(lambda c: func(c).reindex(df.index), result_type='broadcast')
    else:
        raise

Prevention

When it happens

Trigger: df.apply(func, result_type='broadcast') where func returns a 1-D array/Series whose len differs from len(df). For example, func does a groupby/agg that changes length, or returns c.dropna() which shortens the column. Hit at apply.py:1268-1269.

Common situations: Func calls .value_counts(), .unique(), .dropna(), or .sample() on the column, changing its length; returning a derived array indexed differently than the frame; broadcasting expectations mismatched with a downsample/aggregation step.

Related errors


AI-assisted analysis of pandas-dev/pandas@71959b8cb9 (2026-08-07). Data as JSON: /api/errors/b5230d09cdd098e7. Report an issue: GitHub.