pandas-dev/pandas · error · ValueError
Cannot divide vectors with unequal lengths
Error message
Cannot divide vectors with unequal lengths
What it means
Raised by _cast_divlike_op when dividing a timedelta array by another array of differing length. Lengths must match for elementwise division; mismatched vectors are rejected explicitly.
Source
Thrown at pandas/core/arrays/timedeltas.py:660
if op in [roperator.rtruediv, roperator.rfloordiv]:
raise TypeError(
f"Cannot divide {type(other).__name__} by {type(self).__name__}"
)
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 71959b8cb9)
Solutions
- Align indices/lengths via reindex before dividing.
- Use a scalar divisor if all elements share the same scale.
- Verify len(other) == len(self) and document the expectation.
Example fix
// before out = td_series / other_series # mismatched length // after other = other_series.reindex(td_series.index) out = td_series / other
Defensive patterns
Strategy: validation
Validate before calling
assert len(td) == len(other), f'vector length mismatch: {len(td)} vs {len(other)}' Type guard
def div_lengths_match(a, b) -> bool:
return len(a) == len(b) Try / catch
try:
out = td / other
except ValueError as e:
if 'unequal lengths' in str(e):
other = other.reindex(td.index) if hasattr(other, 'reindex') else other[:len(td)]
out = td / other
else:
raise Prevention
- Align operands before dividing.
- Use scalar divisors where possible.
- Verify lengths at function entry.
When it happens
Trigger: `td_array / np.array([...])` where lengths differ; `td_series / other_series` with different index lengths and no alignment.
Common situations: Index misalignment after filter/merge; subsetting one operand but not the other; passing arrays from different sources.
Related errors
- Cannot multiply with unequal lengths
- Function did not transform
- Values resolution does not match dtype.
- Must provide freq argument if no data is supplied
- Of the four parameters: start, end, periods, and freq, exact
AI-assisted analysis of pandas-dev/pandas@71959b8cb9 (2026-08-07).
Data as JSON: /api/errors/2346f26683cd4119.
Report an issue: GitHub.