pandas-dev/pandas · error · ValueError
Length of indexer and values mismatch
Error message
Length of indexer and values mismatch
What it means
In the integer-key branch of __setitem__, after the index has been bounds-checked, if value is list-like (not a scalar and not a pa.Scalar) pandas raises ValueError because assigning a sequence to a single position is a length mismatch. There is no broadcasting for one slot; value must be scalar.
Solutions
- Assign a scalar: s.iloc[3] = value[0] or pick the single element you meant to write.
- If you intended to write multiple values, use a slice or boolean mask key of matching length.
- Verify value is scalar with pandas.api.types.is_scalar(value) before assigning to a single index.
Example fix
// before s = pd.Series([1, 2, 3], dtype="int64[pyarrow]") s.iloc[0] = [99] // after s = pd.Series([1, 2, 3], dtype="int64[pyarrow]") s.iloc[0] = 99
Defensive patterns
Strategy: validation
Validate before calling
from pandas.api.types import is_scalar
def safe_scalar_setitem(arr, key, value):
if not is_scalar(value) and not hasattr(value, "as_py"):
raise ValueError("scalar position requires scalar value")
arr[key] = value Type guard
from pandas.api.types import is_scalar
def value_is_scalar_like(value) -> bool:
return is_scalar(value) or hasattr(value, "as_py") Try / catch
try:
s.iloc[i] = value
except ValueError as e:
if "Length of indexer and values mismatch" in str(e) and is_scalar_int_key:
s.iloc[i] = value[0]
else:
raise Prevention
- Pass only scalars when assigning to a single integer position.
- Use slice/mask keys for list-like values.
When it happens
Trigger: s.iloc[3] = [10, 20] or arr[5] = some_list on an ArrowExtensionArray, where key is an integer but value is multi-element.
Common situations: Confusing scalar-position assignment with slice/boolean-mask assignment; passing a column-of-lists value to a single cell; off-by-one in selecting a scalar from a sequence.
Related errors
- cannot broadcast result
- Cannot divide vectors with unequal lengths
- Cannot multiply with unequal lengths
- index is out of bounds for axis 0 with size
- Lengths must match.
AI-assisted analysis of pandas-dev/pandas@3b7651241d (2026-08-11).
Data as JSON: /api/errors/1d5e7b952f8e6dcf.
Report an issue: GitHub.
Appendix: source
Thrown at pandas/core/arrays/arrow/array.py:2923
):
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 3b7651241d)