{"record":{"id":"099ff22eaa53b4fb","repo":"pandas-dev/pandas","slug":"invalid-value-item-for-dtype-str-value-shou","errorCode":null,"errorMessage":"Invalid value '{item}' for dtype 'str'. Value should be a string or missing value, got '{type(item).__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":315,"sourceCode":"\n    @classmethod\n    def _from_sequence_of_strings(\n        cls, strings, *, dtype: ExtensionDtype, copy: bool = False\n    ) -> Self:\n        return cls._from_sequence(strings, dtype=dtype, copy=copy)\n\n    @property\n    def dtype(self) -> StringDtype:  # type: ignore[override]\n        \"\"\"\n        An instance of 'string[pyarrow]'.\n        \"\"\"\n        return self._dtype\n\n    def insert(self, loc: int, item) -> ArrowStringArray:\n        if self.dtype.na_value is np.nan and item is np.nan:\n            item = libmissing.NA\n        if not isinstance(item, str) and item is not libmissing.NA:\n            raise TypeError(\n                f\"Invalid value '{item}' for dtype 'str'. Value should be a \"\n                f\"string or missing value, got '{type(item).__name__}' instead.\"\n            )\n        return super().insert(loc, item)\n\n    def _convert_bool_result(self, values, na=lib.no_default, method_name=None):\n        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","sourceCodeStart":297,"sourceCodeEnd":333,"githubUrl":"https://github.com/pandas-dev/pandas/blob/3b7651241d4da534b3559b60ef128e1c34f54116/pandas/core/arrays/string_arrow.py#L297-L333","documentation":"ArrowStringArray.insert rejects any item that is neither a Python str nor libmissing.NA. Because the dtype is strictly str, inserting integers, floats, lists, or None-as-nothing raises TypeError to prevent silent coercion that would corrupt the typed array.","triggerScenarios":"arr.insert(loc, 5); arr.insert(loc, 3.14); arr.insert(loc, None) when na_value is not np.nan; arr.insert(loc, ['a']) on an ArrowStringArray.","commonSituations":"Inserting a value from an untyped source (JSON, CSV cell) into a string column without converting; mixing dtypes in a loop that inserts heterogeneous rows.","solutions":["Convert the item to str before inserting: arr.insert(loc, str(value)).","Use libmissing.NA (or pd.NA) for missing values; if na_value is np.nan, np.nan is accepted and converted to NA.","Filter out or route non-string items to a different column."],"exampleFix":"// before\narr.insert(0, 42)  # TypeError\n// after\narr.insert(0, str(42))\n# missing\narr.insert(0, pd.NA)","handlingStrategy":"type-guard","validationCode":"def safe_insert(arr, loc, item):\n    if not isinstance(item, str) and item is not pd.NA:\n        item = str(item)\n    return arr.insert(loc, item)","typeGuard":"def is_insertable_string_item(item) -> bool:\n    return isinstance(item, str) or item is pd.NA","tryCatchPattern":"try:\n    arr.insert(loc, item)\nexcept TypeError as e:\n    if \"Invalid value\" in str(e):\n        arr.insert(loc, str(item))\n    else:\n        raise","preventionTips":["Coerce non-string items to str before insert.","Use pd.NA for missing entries.","Avoid routing non-string data into string[pyarrow] columns."],"tags":["pandas","arrow-string-array","insert","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"}