pandas-dev/pandas · error · ValueError
Length of indexer and values mismatch
Error message
Length of indexer and values mismatch
What it means
Raised on the integer-key setitem path when the value is list-like. Assigning to a single scalar position requires a scalar value; passing a sequence is a shape mismatch pandas rejects immediately.
Source
Thrown at pandas/core/arrays/arrow/array.py:2898
):
data = value
else:
data = self._if_else(True, value, self._pa_array)
elif is_integer(key):
# fast path
key = cast("int", key)
n = len(self)
if key < 0:
key += n
if not 0 <= key < n:
raise IndexError(
f"index {key} is out of bounds for axis 0 with size {n}"
)
if isinstance(value, pa.Scalar):
value = value.as_py()
elif is_list_like(value):
raise ValueError("Length of indexer and values mismatch")
chunks = [
*self._pa_array[:key].chunks,
pa.array([value], type=self._pa_array.type, from_pandas=is_nan_na()),
*self._pa_array[key + 1 :].chunks,
]
data = pa.chunked_array(chunks).combine_chunks()
elif is_bool_dtype(key):
key = np.asarray(key, dtype=np.bool_)
data = self._replace_with_mask(self._pa_array, key, value)
elif is_scalar(value) or isinstance(value, pa.Scalar):
mask = np.zeros(len(self), dtype=np.bool_)
mask[key] = True
data = self._if_else(mask, value, self._pa_array)
else:
indices = np.arange(len(self))[key]View on GitHub (pinned to 71959b8cb9)
Solutions
- Pass a scalar: `arr[k] = scalar`.
- If you intended to assign multiple, use a matching-length index array: `arr[[k1, k2, k3]] = values`.
- Extract the scalar element from the sequence before assignment.
Example fix
// before arr = pd.array([1, 2, 3], dtype="int64[pyarrow]") arr[0] = [10, 20] // after arr[[0, 1]] = [10, 20]
Defensive patterns
Strategy: type-guard
Validate before calling
from pandas.api.types import is_scalar
def coerce_scalar_value(arr, k, value):
if not is_scalar(value):
raise ValueError("single integer index requires scalar value")
return value Type guard
def is_scalar_value(value) -> bool:
from pandas.api.types import is_scalar
return bool(is_scalar(value)) Try / catch
try:
arr[k] = value
except ValueError as e:
if "Length of indexer and values mismatch" in str(e) and isinstance(value, (list, tuple)):
arr[[k] * len(value)] = value
else:
raise Prevention
- Always pass scalars to single-position integer assignment.
- Use array indexers when assigning sequences.
- Add unit tests covering single vs multi assignment paths.
When it happens
Trigger: Doing `arr[k] = [a, b, c]` (k integer) — assigning a list/array to a single positional slot of an ArrowExtensionArray.
Common situations: Loop bugs that pass a row of values to a single index, or confusing scalar vs array assignment semantics.
Related errors
- index {key} is out of bounds for axis 0 with size {n}
- cannot do a non-empty take
- out of bounds value in 'indices'.
- Function did not transform
- putmask: mask and data must be the same size
AI-assisted analysis of pandas-dev/pandas@71959b8cb9 (2026-08-07).
Data as JSON: /api/errors/1d5e7b952f8e6dcf.
Report an issue: GitHub.