{"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":309,"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":291,"sourceCodeEnd":327,"githubUrl":"https://github.com/pandas-dev/pandas/blob/71959b8cb9b2459c16e14b34f28b178ccfe14735/pandas/core/arrays/string_arrow.py#L291-L327","documentation":"Raised by ArrowStringArray.insert() when the item to insert is neither a str instance nor the missing sentinel libmissing.NA. The string[pyarrow] dtype is strictly typed, so inserting an int, float, None (vs NA), or any non-string scalar is rejected at the boundary rather than silently coerced. Callers must explicitly convert or filter non-string values.","triggerScenarios":"Calling `arr.insert(loc, 5)`, `arr.insert(loc, None)`, or `arr.insert(loc, 3.14)` on an ArrowStringArray. The guard at string_arrow.py:308 checks `not isinstance(item, str) and item is not libmissing.NA`.","commonSituations":"Building mixed-type columns dynamically; reading user input that was not sanitized to strings; confusing None (Python null) with pandas NA; passing a numpy.str_ that is actually fine but a numpy int that is not.","solutions":["Convert the item to str before inserting: `arr.insert(loc, str(item))`.","Use `pd.NA` (the string dtype's missing value) instead of `None` to represent missingness.","Filter or coerce upstream so the insert site only ever sees str/pd.NA.","If you genuinely need mixed types, use dtype=object instead of 'string[pyarrow]'."],"exampleFix":"# before\narr.insert(0, 42)  # TypeError\n# after\narr.insert(0, str(42))\n# or for missing:\narr.insert(0, pd.NA)","handlingStrategy":"type-guard","validationCode":"import pandas as pd\nfrom pandas._libs import missing as libmissing\n\ndef safe_insert(arr, loc, item):\n    if item is not libmissing.NA and not isinstance(item, str):\n        item = str(item)\n    return arr.insert(loc, item)","typeGuard":"from pandas._libs import missing as libmissing\nimport numpy as np\ndef is_valid_string_item(item) -> bool:\n    return isinstance(item, str) or item is libmissing.NA or item is pd.NA","tryCatchPattern":"try:\n    arr.insert(loc, item)\nexcept TypeError as e:\n    if 'Invalid value' in str(e) and 'dtype' in str(e):\n        arr.insert(loc, str(item))\n    else:\n        raise","preventionTips":["Sanitize all dynamic insert inputs through str() at the source.","Use pd.NA consistently for missing string values, not None.","Add a typed helper that wraps insert with validation."],"tags":["string-arrow","insert","typeerror","missing-values"],"analyzedSha":"71959b8cb9b2459c16e14b34f28b178ccfe14735","analyzedAt":"2026-08-07T01:30:20.476Z","schemaVersion":2},"datasetVersion":"2026-08-07T03:17:09.362Z"}