{"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":334,"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":316,"sourceCodeEnd":352,"githubUrl":"https://github.com/pandas-dev/pandas/blob/71959b8cb9b2459c16e14b34f28b178ccfe14735/pandas/core/arrays/string_arrow.py#L316-L352","documentation":"Raised by ArrowStringArray._validate_setitem_value() when assigning a scalar that is not a str and not NA (after isna already handled NaN/NaT/None). This guards __setitem__ and similar assignment paths so a string[pyarrow] array cannot be corrupted by a non-string scalar. Unlike object dtype, no implicit coercion happens; the caller must convert explicitly.","triggerScenarios":"Executing `s.iloc[0] = 5` or `arr[0] = 3.14` on a Series/array with dtype 'string[pyarrow]'. The scalar branch at string_arrow.py:333 raises after is_scalar and isna checks pass but isinstance(value, str) fails.","commonSituations":"Assigning numeric query results into a string column; filling with a sentinel int instead of a string/NA; loops that write heterogeneous values into a typed column.","solutions":["Wrap the assigned value in str(): `s.iloc[0] = str(value)`.","Use pd.NA for missing assignments rather than 0, -1, or None.","Coerce the source column to string before assignment: `s.iloc[0] = other.astype('string').iloc[0]`.","If mixed scalar types are truly needed, declare the column as dtype=object."],"exampleFix":"# before\ns = pd.Series(['a','b'], dtype='string[pyarrow]')\ns.iloc[0] = 100  # TypeError\n# after\ns.iloc[0] = str(100)","handlingStrategy":"type-guard","validationCode":"import pandas as pd\nfrom pandas.api.types import is_string_dtype\n\ndef safe_setitem_scalar(arr, loc, value):\n    if pd.isna(value):\n        value = pd.NA\n    elif not isinstance(value, str):\n        value = str(value)\n    arr[loc] = value","typeGuard":"import pandas as pd\ndef is_assignable_string_scalar(v) -> bool:\n    return isinstance(v, str) or pd.isna(v)","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":["Coerce source columns to 'string[pyarrow]' before bulk assignment.","Replace numeric sentinels with pd.NA at ingestion time.","Unit-test assignment paths with mixed input types."],"tags":["string-arrow","setitem","typeerror","dtype-coercion"],"analyzedSha":"71959b8cb9b2459c16e14b34f28b178ccfe14735","analyzedAt":"2026-08-07T01:30:20.476Z","schemaVersion":2},"datasetVersion":"2026-08-07T03:17:09.362Z"}