{"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":348,"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":330,"sourceCodeEnd":366,"githubUrl":"https://github.com/pandas-dev/pandas/blob/71959b8cb9b2459c16e14b34f28b178ccfe14735/pandas/core/arrays/string_arrow.py#L330-L366","documentation":"Raised by ArrowStringArray._validate_setitem_value() for the array-like branch: when the assigned sequence is not 1-dimensional or is not composed entirely of strings/missing values. The check at string_arrow.py:345 uses lib.is_string_array(value, skipna=True) and ndim==1, so a list/tuple/ndarray containing any non-string non-NA element (e.g. an int or float) is rejected.","triggerScenarios":"Assigning `s[:] = [1, 2, 3]`, `s[:] = np.array([1.0, 2.0])`, or a 2-D array to a 'string[pyarrow]' Series. Triggered when the value passes is_scalar (False) and is not an ArrowStringArray, landing in the array validation branch.","commonSituations":"Bulk-assigning a numeric column's values into a string column; replacing a slice with output of a numeric reduction; feeding unparsed JSON/CSV cells directly.","solutions":["Convert the sequence element-wise to str first: `s[:] = [str(x) for x in values]` or `pd.array(values, dtype='string[pyarrow]')`.","If the source is a Series, cast it: `s[:] = other.astype('string[pyarrow]')`.","Replace any non-string sentinels with pd.NA before assignment.","Ensure the value is 1-D; reshape or flatten 2-D inputs explicitly."],"exampleFix":"# before\ns = pd.Series(['a','b','c'], dtype='string[pyarrow]')\ns[:] = [1, 2, 3]  # TypeError\n# after\ns[:] = [str(x) for x in [1, 2, 3]]","handlingStrategy":"validation","validationCode":"import numpy as np\nfrom pandas._libs import lib\n\ndef safe_setitem_array(arr, loc, values):\n    values = np.asarray(values, dtype=object)\n    if values.ndim != 1 or not lib.is_string_array(values, skipna=True):\n        values = np.array([str(x) for x in values], dtype=object)\n    arr[loc] = values","typeGuard":"import numpy as np\nfrom pandas._libs import lib\ndef is_1d_string_array(values) -> bool:\n    arr = np.asarray(values, dtype=object)\n    return arr.ndim == 1 and lib.is_string_array(arr, skipna=True)","tryCatchPattern":"try:\n    s[:] = values\nexcept TypeError as e:\n    if 'Invalid value for dtype' in str(e):\n        s[:] = [str(x) for x in values]\n    else:\n        raise","preventionTips":["Cast assignment sources via pd.array(values, dtype='string[pyarrow]').","Flatten 2-D inputs explicitly before assignment.","Validate ndim==1 and string content before bulk setitem."],"tags":["string-arrow","setitem","array-assignment","typeerror"],"analyzedSha":"71959b8cb9b2459c16e14b34f28b178ccfe14735","analyzedAt":"2026-08-07T01:30:20.476Z","schemaVersion":2},"datasetVersion":"2026-08-07T03:17:09.362Z"}