{"record":{"id":"1d5e7b952f8e6dcf","repo":"pandas-dev/pandas","slug":"length-of-indexer-and-values-mismatch","errorCode":null,"errorMessage":"Length of indexer and values mismatch","messagePattern":"Length of indexer and values mismatch","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"pandas/core/arrays/arrow/array.py","lineNumber":2923,"sourceCode":"            ):\n                data = value\n            else:\n                data = self._if_else(True, value, self._pa_array)\n\n        elif is_integer(key):\n            # fast path\n            key = cast(\"int\", key)\n            n = len(self)\n            if key < 0:\n                key += n\n            if not 0 <= key < n:\n                raise IndexError(\n                    f\"index {key} is out of bounds for axis 0 with size {n}\"\n                )\n            if isinstance(value, pa.Scalar):\n                value = value.as_py()\n            elif is_list_like(value):\n                raise ValueError(\"Length of indexer and values mismatch\")\n            chunks = [\n                *self._pa_array[:key].chunks,\n                pa.array([value], type=self._pa_array.type, from_pandas=is_nan_na()),\n                *self._pa_array[key + 1 :].chunks,\n            ]\n            data = pa.chunked_array(chunks).combine_chunks()\n\n        elif is_bool_dtype(key):\n            key = np.asarray(key, dtype=np.bool_)\n            data = self._replace_with_mask(self._pa_array, key, value)\n\n        elif is_scalar(value) or isinstance(value, pa.Scalar):\n            mask = np.zeros(len(self), dtype=np.bool_)\n            mask[key] = True\n            data = self._if_else(mask, value, self._pa_array)\n\n        else:\n            indices = np.arange(len(self))[key]","sourceCodeStart":2905,"sourceCodeEnd":2941,"githubUrl":"https://github.com/pandas-dev/pandas/blob/3b7651241d4da534b3559b60ef128e1c34f54116/pandas/core/arrays/arrow/array.py#L2905-L2941","documentation":"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.","triggerScenarios":"s.iloc[3] = [10, 20] or arr[5] = some_list on an ArrowExtensionArray, where key is an integer but value is multi-element.","commonSituations":"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.","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."],"exampleFix":"// before\ns = pd.Series([1, 2, 3], dtype=\"int64[pyarrow]\")\ns.iloc[0] = [99]\n// after\ns = pd.Series([1, 2, 3], dtype=\"int64[pyarrow]\")\ns.iloc[0] = 99","handlingStrategy":"validation","validationCode":"from pandas.api.types import is_scalar\n\ndef safe_scalar_setitem(arr, key, value):\n    if not is_scalar(value) and not hasattr(value, \"as_py\"):\n        raise ValueError(\"scalar position requires scalar value\")\n    arr[key] = value","typeGuard":"from pandas.api.types import is_scalar\n\ndef value_is_scalar_like(value) -> bool:\n    return is_scalar(value) or hasattr(value, \"as_py\")","tryCatchPattern":"try:\n    s.iloc[i] = value\nexcept ValueError as e:\n    if \"Length of indexer and values mismatch\" in str(e) and is_scalar_int_key:\n        s.iloc[i] = value[0]\n    else:\n        raise","preventionTips":["Pass only scalars when assigning to a single integer position.","Use slice/mask keys for list-like values."],"tags":["pyarrow","setitem","length-mismatch","valueerror"],"backgroundTag":null,"analyzedSha":"3b7651241d4da534b3559b60ef128e1c34f54116","analyzedAt":"2026-08-11T22:10:44.015Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}