{"record":{"id":"4554fdfe59597734","repo":"pandas-dev/pandas","slug":"invalid-value-for-dtype-str-value-should-be-a-s-4554fd","errorCode":null,"errorMessage":"Invalid value for dtype 'str'. Value should be a string or missing value (or array of those).","messagePattern":"Invalid value for dtype 'str'\\. Value should be a string or missing value \\(or array of those\\)\\.","errorType":"exception","errorClass":"TypeError","httpStatus":null,"severity":"error","filePath":"pandas/core/arrays/string_arrow.py","lineNumber":354,"sourceCode":"        if is_scalar(value):\n            if isna(value):\n                value = None\n            elif not isinstance(value, str):\n                raise TypeError(\n                    f\"Invalid value '{value}' for dtype 'str'. Value should be a \"\n                    f\"string or missing value, got '{type(value).__name__}' instead.\"\n                )\n        elif isinstance(value, type(self)):\n            pass\n        else:\n            if not is_array_like_deprecate_non_pandas(value):\n                value = np.asarray(value, dtype=object)\n            else:\n                value = np.asarray(value)\n            if len(value) and not (\n                value.ndim == 1 and lib.is_string_array(value, skipna=True)\n            ):\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 super()._validate_setitem_value(value)\n\n    def isin(self, values: ArrayLike) -> npt.NDArray[np.bool_]:\n        value_set = [\n            pa_scalar.as_py()\n            for pa_scalar in [pa.scalar(value, from_pandas=True) for value in values]\n            if pa_scalar.type in (pa.string(), pa.null(), pa.large_string())\n        ]\n\n        # short-circuit to return all False array.\n        if not value_set:\n            return np.zeros(len(self), dtype=bool)\n\n        result = pc.is_in(\n            self._pa_array, value_set=pa.array(value_set, type=self._pa_array.type)","sourceCodeStart":336,"sourceCodeEnd":372,"githubUrl":"https://github.com/pandas-dev/pandas/blob/3b7651241d4da534b3559b60ef128e1c34f54116/pandas/core/arrays/string_arrow.py#L336-L372","documentation":"ArrowStringArray._validate_setitem_value rejects array-likes whose contents are not all strings (or NAs). After converting the value to a numpy object array, it checks lib.is_string_array(value, skipna=True); any non-string non-NA element triggers TypeError.","triggerScenarios":"s.iloc[:] = [1, 'a', 'b'] on a string[pyarrow] Series; s.iloc[:] = np.array([1.0, 2.0]) (non-string ndarray); assigning a list with mixed types.","commonSituations":"Assigning a list comprehension that yields mixed types; vectorized assignment from another column with the wrong dtype; bulk replacement with computed non-string values.","solutions":["Coerce each element to str: s.iloc[:] = [str(v) for v in values].","Use pd.NA in place of None/NaN for missing entries.","Validate the source array with pd.api.types.is_string_dtype before assignment."],"exampleFix":"// before\ns.iloc[:] = [1, 2, 3]  # TypeError\n// after\ns.iloc[:] = [str(v) for v in [1, 2, 3]]","handlingStrategy":"validation","validationCode":"def safe_setitem_array(arr, key, values):\n    import numpy as np\n    values = np.asarray(values, dtype=object)\n    if len(values) and not all(isinstance(v, str) or v is pd.NA for v in values):\n        values = np.array([str(v) if not (isinstance(v, str) or v is pd.NA) else v for v in values], dtype=object)\n    arr[key] = values","typeGuard":"def is_string_or_na_array(values) -> bool:\n    import numpy as np\n    arr = np.asarray(values, dtype=object)\n    return all(isinstance(v, str) or v is pd.NA for v in arr)","tryCatchPattern":"try:\n    s.iloc[:] = values\nexcept TypeError as e:\n    if \"Invalid value for dtype 'str'\" in str(e):\n        s.iloc[:] = [str(v) for v in values]\n    else:\n        raise","preventionTips":["Coerce each element to str before bulk assignment.","Use pd.NA consistently for missing entries.","Validate source dtype before assignment."],"tags":["pandas","arrow-string-array","setitem","array","dtype"],"backgroundTag":null,"analyzedSha":"3b7651241d4da534b3559b60ef128e1c34f54116","analyzedAt":"2026-08-11T22:10:44.015Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}