{"record":{"id":"0c7b5c9f5e4d1d63","repo":"pandas-dev/pandas","slug":"invalid-value-value-for-dtype-str-value-sho","errorCode":null,"errorMessage":"Invalid value '{value}' for dtype 'str'. Value should be a string or missing value, got '{type(value).__name__}' instead.","messagePattern":"Invalid value '(.+?)' for dtype 'str'\\. Value should be a string or missing value, got '(.+?)' instead\\.","errorType":"exception","errorClass":"TypeError","httpStatus":null,"severity":"error","filePath":"pandas/core/arrays/string_arrow.py","lineNumber":340,"sourceCode":"        validate_na_arg(na, name=\"na\")\n        if self.dtype.na_value is np.nan:\n            if na is lib.no_default or isna(na):\n                # NaN propagates as False\n                values = values.fill_null(False)\n            else:\n                values = values.fill_null(na)\n            return values.to_numpy()\n        elif na is not lib.no_default and not isna(na):  # pyright: ignore [reportGeneralTypeIssues]\n            values = values.fill_null(na)\n        return BooleanDtype().__from_arrow__(values)\n\n    def _validate_setitem_value(self, value):\n        \"\"\"Maybe convert value to be pyarrow compatible.\"\"\"\n        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)","sourceCodeStart":322,"sourceCodeEnd":358,"githubUrl":"https://github.com/pandas-dev/pandas/blob/3b7651241d4da534b3559b60ef128e1c34f54116/pandas/core/arrays/string_arrow.py#L322-L358","documentation":"ArrowStringArray._validate_setitem_value rejects scalar values that are not str and not NA. Setting an int, float, bool, or list scalar into a string[pyarrow] array raises TypeError to keep the strict str dtype.","triggerScenarios":"s.iloc[0] = 5 on a string[pyarrow] Series; s.iloc[0] = 3.14; s.iloc[0] = True; any scalar non-string setitem after isna() check fails.","commonSituations":"Assigning cleaned numeric data into a string column without conversion; replacing a NaN cell with a computed numeric value; mis-typed pipeline stages.","solutions":["Cast the value to str: s.iloc[0] = str(value).","Use pd.NA for missing values.","If the column will hold numbers, change its dtype with astype instead of assigning into a string column."],"exampleFix":"// before\ns.iloc[0] = 100  # TypeError\n// after\ns.iloc[0] = str(100)","handlingStrategy":"type-guard","validationCode":"def safe_setitem_scalar(arr, key, value):\n    from pandas.api.types import is_scalar, isna\n    if is_scalar(value) and not isinstance(value, str) and not isna(value):\n        value = str(value)\n    arr[key] = value","typeGuard":"def is_setitem_valid_string_scalar(value) -> bool:\n    from pandas.api.types import is_scalar, isna\n    return isinstance(value, str) or (is_scalar(value) and isna(value))","tryCatchPattern":"try:\n    s.iloc[i] = value\nexcept TypeError as e:\n    if \"Invalid value\" in str(e):\n        s.iloc[i] = str(value)\n    else:\n        raise","preventionTips":["Cast numeric scalars to str before assignment.","Use pd.NA for missing values.","Switch column dtype if numbers are the intended content."],"tags":["pandas","arrow-string-array","setitem","type-error","dtype"],"backgroundTag":null,"analyzedSha":"3b7651241d4da534b3559b60ef128e1c34f54116","analyzedAt":"2026-08-11T22:10:44.015Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}