pandas-dev/pandas · error · ValueError
Lengths of operands do not match
Error message
Lengths of operands do not match: {len(self)} != {len(other)} What it means
Raised by _str_arith_method_object_fallback when `other` is list-like but its length differs from len(self). This fallback path emulates string arithmetic through object dtype, and it requires the operands to be broadcastable; a length mismatch is a programmer error, not a missing-feature error.
Solutions
- Ensure len(other) == len(arr) before operating, or wrap in a Series so pandas aligns on the index.
- Use a scalar operand for uniform operations.
- Broadcast explicitly: np.broadcast_to(other, len(arr)).
- Align indexes first if both are Series: s1.align(s2).
Example fix
# before s = pd.Series(['a', 'b', 'c'], dtype="string[pyarrow]") s + pd.array(['x', 'y'], dtype="string[pyarrow]") # raises ValueError # after s + pd.array(['x', 'y', 'z'], dtype="string[pyarrow]")
Defensive patterns
Strategy: validation
Validate before calling
def assert_same_length(a, b):
la, lb = len(a), len(b)
if la != lb:
raise ValueError(f'Length mismatch: {la} != {lb}')
return True Type guard
from pandas.api.types import is_list_like
def is_broadcastable(other, target_len: int) -> bool:
return not is_list_like(other) or len(other) == target_len Prevention
- Align indexes via Series before elementwise operations.
- Validate len(other) == len(arr) before fallback arithmetic.
- Prefer scalar operands for uniform operations.
When it happens
Trigger: string_array + list_of_different_length; s + other_series where Indexes/lengths differ and pandas did not align (this fallback is reached after pyarrow raises ArrowInvalid/TypeError); passing a tuple or 2-D array as the other operand.
Common situations: Manual elementwise operations bypassing Series alignment; feeding a raw list whose length was computed independently; off-by-one in generating the operand list; numpy broadcasting assumptions that do not hold for object fallback.
Related errors
- Can only string multiply by an integer.
- empty separator
- length mismatch: vs.
- Length of 'value' does not match. Got
- Lengths of operands do not match
AI-assisted analysis of pandas-dev/pandas@3b7651241d (2026-08-11).
Data as JSON: /api/errors/b31eb84ac7c3142c.
Report an issue: GitHub.
Appendix: source
Thrown at pandas/core/arrays/arrow/array.py:1290
f"'{op_name}' operations between boolean dtype and {self.dtype} are "
"deprecated and will raise in a future version. Explicitly "
"cast the strings to a boolean dtype before operating instead.",
Pandas4Warning,
stacklevel=find_stack_level(),
)
return op(other, self.astype(bool))
else:
return self._evaluate_op_method(other, op, ARROW_LOGICAL_FUNCS)
def _str_arith_method_object_fallback(
self, other, op
) -> Self | npt.NDArray[np.object_]:
mask = isna(self) | isna(other)
valid = ~mask
if is_list_like(other):
if len(other) != len(self):
raise ValueError(
f"Lengths of operands do not match: {len(self)} != {len(other)}"
)
if not is_array_like_deprecate_non_pandas(other):
other = np.asarray(other)
other = other[valid]
result = np.empty(len(self), dtype=object)
result[mask] = self.dtype.na_value
result[valid] = op(np.asarray(self, dtype=object)[valid], other)
if not lib.is_string_array(result, skipna=True):
return result
return type(self)._from_sequence(result, dtype=self.dtype)
def _arith_method(self, other, op) -> Self | npt.NDArray[np.object_]:
if isinstance(other, BaseOffset) and pa.types.is_date(self._pa_array.type):
# Cast date32/date64 → timestamp, apply offset via DatetimeArray, cast back
ts_array = type(self)(self._pa_array.cast(pa.timestamp("us")))View on GitHub (pinned to 3b7651241d)