pandas-dev/pandas · error · ValueError
cannot broadcast result
Error message
cannot broadcast result
What it means
Raised inside `apply_broadcast` when a per-column UDF result is 1-D but its length does not equal `target.shape[0]` (the frame's row count). Broadcast mode places the result back into a column of fixed length, so a length mismatch is rejected rather than silently truncated or padded.
Solutions
- Ensure the UDF returns exactly `len(df)` values per column (do not filter inside the UDF).
- If filtering is needed, perform it before the apply or reindex the result to the original index inside the UDF.
- Use `df.transform(func)` instead of `df.apply(func, result_type='broadcast')` if you want index-aligned same-length outputs — transform enforces the contract with a clearer message.
Example fix
// before df.apply(lambda s: s.dropna(), result_type='broadcast') // after df.apply(lambda s: s.dropna().reindex(df.index), result_type='broadcast')
Defensive patterns
Strategy: validation
Validate before calling
def broadcast_apply_length_safe(df, func):
n = len(df)
for col in df.columns:
res = func(df[col].head(2))
import numpy as np
arr = np.asarray(res)
if arr.ndim == 1 and len(arr) != n:
raise ValueError(f'{func.__name__} returns length {len(arr)} for column {col!r}; expected {n}')
return df.apply(func, result_type='broadcast') Type guard
def func_preserves_length(func, series) -> bool:
import numpy as np
try:
arr = np.asarray(func(series))
return arr.ndim <= 1 and len(arr) == len(series)
except Exception:
return False Try / catch
try:
out = df.apply(func, result_type='broadcast')
except ValueError as e:
if 'cannot broadcast result' in str(e):
out = df.transform(func) # transform enforces same-length contract more clearly
else:
raise Prevention
- Never filter rows (dropna/head) inside a broadcast UDF.
- Reindex UDF results back to df.index before returning.
- Prefer df.transform for same-length elementwise ops.
When it happens
Trigger: `df.apply(func, result_type='broadcast')` where `func(df[col])` returns a 1-D array/Series whose `len(...)` differs from the number of rows. E.g. `df.apply(lambda s: s.dropna().values, result_type='broadcast')` (drops NaNs, shorter than input), or `lambda s: s.head(5)`.
Common situations: UDFs that filter (`dropna`, `drop_duplicates`, `head`), resample, or otherwise change length inside a broadcast apply; assuming broadcast will pad/align like a transform.
Related errors
- too many dims to broadcast
- by_row= not allowed
- Column is backed by an extension array, which is not…
- Column must have a numeric dtype. Found ' ' instead
- invalid value for result_type, must be one of
AI-assisted analysis of pandas-dev/pandas@3b7651241d (2026-08-11).
Data as JSON: /api/errors/b5230d09cdd098e7.
Report an issue: GitHub.
Appendix: 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 3b7651241d)