{"record":{"id":"f71d5b7340c839fa","repo":"pandas-dev/pandas","slug":"setting-an-array-element-with-a-sequence","errorCode":null,"errorMessage":"setting an array element with a sequence.","messagePattern":"setting an array element with a sequence\\.","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"pandas/core/arrays/string_.py","lineNumber":879,"sourceCode":"                value = np.asarray(value)\n            if len(value) and not lib.is_string_array(value, skipna=True):\n                raise TypeError(\n                    \"Invalid value for dtype 'str'. Value should be a \"\n                    \"string or missing value (or array of those).\"\n                )\n        return value\n\n    def __setitem__(self, key, value) -> None:\n        if self._readonly:\n            raise ValueError(\"Cannot modify read-only array\")\n\n        value = self._validate_setitem_value(value)\n\n        key = check_array_indexer(self, key)\n        scalar_key = lib.is_scalar(key)\n        scalar_value = lib.is_scalar(value)\n        if scalar_key and not scalar_value:\n            raise ValueError(\"setting an array element with a sequence.\")\n\n        if not scalar_value:\n            if value.dtype == self.dtype:\n                value = value._ndarray\n            else:\n                value = np.asarray(value)\n                mask = isna(value)\n                if mask.any():\n                    value = value.copy()\n                    value[isna(value)] = self.dtype.na_value\n\n        super().__setitem__(key, value)\n\n    def _putmask(self, mask: npt.NDArray[np.bool_], value) -> None:\n        # the super() method NDArrayBackedExtensionArray._putmask uses\n        # np.putmask which doesn't properly handle None/pd.NA, so using the\n        # base class implementation that uses __setitem__\n        ExtensionArray._putmask(self, mask, value)","sourceCodeStart":861,"sourceCodeEnd":897,"githubUrl":"https://github.com/pandas-dev/pandas/blob/71959b8cb9b2459c16e14b34f28b178ccfe14735/pandas/core/arrays/string_.py#L861-L897","documentation":"StringArray.__setitem__ raises ValueError('setting an array element with a sequence.') when a scalar key (single integer/position) is paired with a non-scalar value. You cannot store a list/array into a single slot of a 1-D string array.","triggerScenarios":"Calling string_array[0] = ['a', 'b'], string_array[0] = np.array(['a','b']), or any assignment that puts a sequence into one scalar index.","commonSituations":"Accidentally passing a list where a single value is expected; off-by-one in slicing that collapses to a scalar key while the value stays list-like.","solutions":["Use a slice or array key to place multiple values: arr[0:2] = ['a', 'b'].","Pass a scalar value for a scalar key: arr[0] = 'a'.","Check lib.is_scalar(value) and adjust the key accordingly."],"exampleFix":"// before\nstring_array[0] = ['a', 'b']\n\n// after\nstring_array[0:2] = ['a', 'b']","handlingStrategy":"validation","validationCode":"import numpy as np\nfrom pandas._libs import lib\n\nif lib.is_scalar(key) and not lib.is_scalar(value):\n    key = slice(key, key + len(value)) if isinstance(value, (list, np.ndarray)) else key\nstring_array[key] = value","typeGuard":"from pandas._libs import lib\n\ndef key_value_shapes_match(key, value) -> bool:\n    return lib.is_scalar(key) == lib.is_scalar(value)","tryCatchPattern":null,"preventionTips":["Pair scalar keys with scalar values, and slice/array keys with sequences.","Use arr[i:j] = sequence to place multiple values.","Validate that key and value scalar-ness agree before assignment."],"tags":["string-array","setitem","broadcasting","shape"],"analyzedSha":"71959b8cb9b2459c16e14b34f28b178ccfe14735","analyzedAt":"2026-08-07T01:30:20.476Z","schemaVersion":2},"datasetVersion":"2026-08-07T03:17:09.362Z"}