{"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":"TypeError","httpStatus":null,"severity":"error","filePath":"py-polars/src/polars/series/series.py","lineNumber":1433,"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":1415,"sourceCodeEnd":1451,"githubUrl":"https://github.com/pola-rs/polars/blob/fe841f959ef4d2ceefc05a310d33ed7b1ab24e5e/py-polars/src/polars/series/series.py#L1415-L1451","documentation":"Series.__setitem__ raises this TypeError when the value being assigned is a list or tuple but the Series dtype is not one that supports sequence assignment. Polars only accepts scalar values (or matching structures like lists for list-dtype columns) when setting via a key; for this dtype you must use a scalar.","triggerScenarios":"`series[mask_or_indices] = [1, 2, 3]` where series.dtype is e.g. Int32, Date, or another non-list dtype, hitting the branch at py-polars/src/polars/series/series.py:1433.","commonSituations":"Numpy habits (`arr[mask] = [..]`) carried into polars; assigning multiple replacement values at masked positions; pandas-style vectorized assignment of lists.","solutions":["Assign a scalar: `series[mask] = 0` instead of a list","Wrap the list in the appropriate structure only for List-dtype series (e.g. `series[i] = [1,2,3]` on List(Int64))","Use `series.scatter(indices, value_series)` to set multiple positions from another Series"],"exampleFix":"// before\ns[s > 10] = [0, 0, 0]  # TypeError\n// after\ns = pl.when(s > 10).then(0).otherwise(s)","handlingStrategy":"validation","validationCode":"if isinstance(value, (list, tuple)) and not isinstance(series.dtype, pl.List):\n    raise ValueError(\"assign a scalar or use when/then instead of a list value\")\nseries[mask] = value","typeGuard":"def is_scalar(value: object) -> bool:\n    return not isinstance(value, (list, tuple, pl.Series))","tryCatchPattern":"try:\n    series[mask] = value\nexcept TypeError as e:\n    if \"list/tuple as value\" in str(e):\n        series = pl.when(pl.Series([True] * len(series)) & mask).then(value[0]).otherwise(series)\n    else:\n        raise","preventionTips":["Assign scalars via __setitem__; use expressions (when/then) for vectorized replacement","Use Series.scatter for multi-position assignment","Don't copy numpy assignment idioms directly to polars Series"],"tags":["polars","python","setitem","list-value","type-error"],"backgroundTag":"invalid-argument-value","analyzedSha":"fe841f959ef4d2ceefc05a310d33ed7b1ab24e5e","analyzedAt":"2026-09-18T22:14:11.667Z","contentChangedAt":"2026-09-18T22:14:11.667Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}