pandas-dev/pandas · error · ValueError

Cannot divide vectors with unequal lengths

Error message

Cannot divide vectors with unequal lengths

What it means

Raised in TimedeltaArray._cast_divlike_op when the array operand's length differs from the timedelta array's length. This guard runs after list/tuple operands are converted to arrays and before any division dispatch, ensuring elementwise division operands are aligned in length.

Solutions

  1. Ensure both operands have equal length.
  2. Use pandas Series objects so index alignment applies, then handle resulting NaN as needed.
  3. Broadcast a scalar divisor if uniform scaling was intended.

Example fix

# before
pd.to_timedelta([1, 2, 3], unit='D') / np.array([1, 2])  # ValueError

# after
pd.to_timedelta([1, 2, 3], unit='D') / np.array([1, 2, 3])
Defensive patterns

Strategy: validation

Validate before calling

def div_lengths_match(a, b) -> bool:
    return len(a) == len(b)

Type guard

null

Try / catch

try:
    result = td / other
except ValueError as e:
    if 'unequal lengths' in str(e):
        raise
    raise

Prevention

When it happens

Trigger: `pd.to_timedelta([1, 2, 3], unit='D') / np.array([1, 2])`, or dividing two unequal-length arrays/lists.

Common situations: Using raw numpy arrays/lists that bypass index alignment; mismatched divisor vectors.

Related errors


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

Appendix: source

Thrown at pandas/core/arrays/timedeltas.py:680

            if op in [roperator.rtruediv, roperator.rfloordiv]:
                raise TypeError(
                    f"Cannot divide {type(other).__name__} by {type(self).__name__}"
                )

            other = _exact_if_integral(other)
            if lib.is_float(other):
                # GH#43178: raise instead of silently saturating on overflow
                self._check_float_div_overflow(other)
            result = op(self._ndarray, other)
            return type(self)._simple_new(result, dtype=result.dtype)

    def _cast_divlike_op(self, other):
        if not hasattr(other, "dtype"):
            # e.g. list, tuple
            other = np.array(other)

        if len(other) != len(self):
            raise ValueError("Cannot divide vectors with unequal lengths")
        return other

    def _vector_divlike_op(self, other, op) -> np.ndarray | Self:
        """
        Shared logic for __truediv__, __floordiv__, and their reversed versions
        with timedelta64-dtype ndarray other.
        """
        other_arr = np.asarray(other)
        if other_arr.dtype.kind == "f" and op in [operator.truediv, operator.floordiv]:
            # GH#43178: raise instead of silently saturating on overflow
            self._check_float_div_overflow(other_arr)

        # Let numpy handle it
        result = op(self._ndarray, other_arr)

        if (is_integer_dtype(other.dtype) or is_float_dtype(other.dtype)) and op in [
            operator.truediv,
            operator.floordiv,

View on GitHub (pinned to 3b7651241d)