pandas-dev/pandas · error · ValueError

too many dims to broadcast

Error message

too many dims to broadcast

What it means

Raised inside DataFrame.apply_broadcast (apply.py:1265) when the user-supplied func, applied to a column under result_type='broadcast', returns an array with more than one dimension. Broadcasting requires each per-column return to be a scalar or a 1-D array sized to the frame's row count; a 2-D (or higher) return cannot be assigned back into the single column slice result_values[:, i].

Source

Thrown at pandas/core/apply.py:1265

            return self.obj._constructor(result, index=self.index, columns=self.columns)
        else:
            return self.obj._constructor_sliced(result, index=self.agg_axis)

    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()

View on GitHub (pinned to 71959b8cb9)

Solutions

  1. Make func return a scalar or a 1-D array/Series per column. If it currently returns a 2-D array, flatten with .ravel() or .squeeze().
  2. If you genuinely need multiple output columns, switch away from result_type='broadcast' to the default apply (which infers columns from a dict/Series return) or use result_type='expand'.
  3. Inspect the return shape with a quick standalone call to func(df[df.columns[0]]) and adjust before running the full apply.

Example fix

// before
df.apply(lambda c: np.reshape(c.values*2, (-1,1)), result_type='broadcast')
// after
df.apply(lambda c: (c.values*2).ravel(), result_type='broadcast')
Defensive patterns

Strategy: validation

Validate before calling

import numpy as np
sample = func(df[df.columns[0]], *args, **kwargs)
if np.asarray(sample).ndim > 1:
    raise ValueError('func must return a scalar or 1-D array when result_type=broadcast')

Type guard

def returns_at_most_1d(func, sample_input) -> bool:
    import numpy as np
    return np.asarray(func(sample_input)).ndim <= 1

Try / catch

try:
    df.apply(func, result_type='broadcast')
except ValueError as e:
    if 'too many dims' in str(e):
        df.apply(lambda c: np.asarray(func(c)).ravel(), result_type='broadcast')
    else:
        raise

Prevention

When it happens

Trigger: df.apply(func, result_type='broadcast') where func returns a 2-D numpy array, DataFrame, or any ndarray with ndim >= 2. Hit in apply_broadcast at apply.py:1261-1265 when np.asarray(res).ndim > 1.

Common situations: Func intended to return a single column but actually returns a reshaped 2-D array (e.g. np.reshape(x, (-1,1))); func computing a DataFrame of multiple columns when broadcast expects one; transposing mistakes that flip the result shape.

Related errors


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