{"record":{"id":"d25b24eece7925ce","repo":"pola-rs/polars","slug":"cannot-set-series-of-dtype-self-dtype-r-with-li","errorCode":null,"errorMessage":"cannot set Series of dtype: {self.dtype!r} with list/tuple as value; use a scalar value","messagePattern":"cannot set Series of dtype: (.+?) with list/tuple as value; use a scalar value","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"py-polars/src/polars/series/series.py","lineNumber":1547,"sourceCode":"        [\n            10\n            99\n            99\n        ]\n        \"\"\"\n        # do the single idx as first branch as those are likely in a tight loop\n        if isinstance(key, int) and not isinstance(key, bool):\n            self.scatter(key, value)\n            return None\n        elif isinstance(value, Sequence) and not isinstance(value, str):\n            if self.dtype.is_numeric() or self.dtype.is_temporal():\n                self.scatter(key, value)  # type: ignore[arg-type]\n                return None\n            msg = (\n                f\"cannot set Series of dtype: {self.dtype!r} with list/tuple as value;\"\n                \" use a scalar value\"\n            )\n            raise TypeError(msg)\n        if isinstance(key, Series):\n            if key.dtype == Boolean:\n                self._s = self.set(key, value)._s\n            elif key.dtype.is_integer():\n                self._s = self.scatter(key, value)._s\n            else:\n                msg = f\"cannot use Series of dtype {key.dtype!r} for indexing; expected boolean or integer dtype\"\n                raise TypeError(msg)\n\n        # TODO: implement for these types without casting to series\n        elif _check_for_numpy(key) and isinstance(key, np.ndarray):\n            if key.dtype == np.bool_:\n                # boolean numpy mask\n                self._s = self.scatter(np.argwhere(key)[:, 0], value)._s\n            else:\n                s = self._from_pyseries(\n                    PySeries.new_u32(\"\", np.array(key, np.uint32), _strict=True)\n                )","sourceCodeStart":1529,"sourceCodeEnd":1565,"githubUrl":"https://github.com/pola-rs/polars/blob/df599052daf96e7a9cc30a3b0c6bd25d6947e3c0/py-polars/src/polars/series/series.py#L1529-L1565","documentation":"Raised by Series.__setitem__ when you assign a list/tuple value to a Series whose dtype is not numeric or temporal. For numeric/temporal dtypes Polars scatters the sequence element-wise, but for String, List, Struct, Categorical, etc. there is no vector path, so it demands a scalar value instead.","triggerScenarios":"`s = pl.Series(['a','b','c']); s[[0,1]] = ['x','y']`, `s[0] = ['x']`, `s[mask] = ('p','q')` on a String/Categorical/List/Struct-typed Series. Numeric and temporal Series accept sequences fine (scatter branch just above).","commonSituations":"Pandas-style multi-cell assignment (`df['col'][[0,1]] = [...]`) ported to Polars; updating string label columns in a loop-free batch; setting a single element but wrapping the value in a list by accident (`s[0] = ['x']` instead of `s[0] = 'x'`).","solutions":["Assign a scalar: `s[0] = 'x'`.","For multiple positions on non-numeric dtypes, loop scalars or rebuild the Series: `pl.Series(name, new_values, dtype=s.dtype)`.","For many updates, prefer expression-based replacement: `s.set_at_idx(idx, values)` (check dtype support) or `s.zip_with(mask, other)`.","If the column is genuinely numeric/temporal, check that it was not mis-typed as String during inference."],"exampleFix":"// before\ns = pl.Series(['a', 'b', 'c'])\ns[[0, 2]] = ['x', 'z']  # TypeError\n\n// after\nfor i, v in zip([0, 2], ['x', 'z']):\n    s[i] = v\n# or rebuild:\npl.Series(s.name, ['x', 'b', 'z'])","handlingStrategy":"validation","validationCode":"def setitem_value_ok(s: pl.Series, value) -> bool:\n    if isinstance(value, (list, tuple)) and not isinstance(value, str):\n        return s.dtype.is_numeric() or s.dtype.is_temporal()\n    return True\n\nassert setitem_value_ok(s, ['x', 'y']), 'use a scalar for this dtype'","typeGuard":"def accepts_sequence_value(s: pl.Series) -> bool:\n    return s.dtype.is_numeric() or s.dtype.is_temporal()","tryCatchPattern":"try:\n    s[key] = value\nexcept TypeError as e:\n    if 'list/tuple as value' not in str(e):\n        raise\n    for i, v in zip(indices, value):\n        s[i] = v","preventionTips":["Only assign scalars to String/Categorical/List/Struct Series.","Batch updates are better expressed as rebuilding the Series or pl.when/then in a frame.","Watch for accidental list-wrapping: s[0] = ['x'] instead of s[0] = 'x'."],"tags":["polars","series","setitem","dtype","scatter","assignment"],"backgroundTag":null,"analyzedSha":"df599052daf96e7a9cc30a3b0c6bd25d6947e3c0","analyzedAt":"2026-08-16T12:10:03.978Z","schemaVersion":2},"datasetVersion":"2026-08-16T13:17:31.715Z"}