{"record":{"id":"9f86da8cf1dce830","repo":"pandas-dev/pandas","slug":"invalid-value-value-s-for-dtype-self-dtype-9f86da","errorCode":null,"errorMessage":"Invalid value '{value!s}' for dtype '{self.dtype}'","messagePattern":"Invalid value '(.+?)' for dtype '(.+?)'","errorType":"exception","errorClass":"TypeError","httpStatus":null,"severity":"error","filePath":"pandas/core/arrays/numpy_.py","lineNumber":185,"sourceCode":"\n        if copy and result is scalars:\n            result = result.copy()\n        return cls(result)\n\n    def _validate_setitem_value(self, value):\n        if isinstance(value, type(self)):\n            value = value._ndarray\n\n        # Match Block._standardize_fill_value behavior\n        if self._ndarray.dtype.kind != \"O\" and is_valid_na_for_dtype(\n            value, self._ndarray.dtype\n        ):\n            value = self.dtype.na_value\n\n        try:\n            return np_can_hold_element(self._ndarray.dtype, value)\n        except LossySetitemError as err:\n            raise TypeError(\n                f\"Invalid value '{value!s}' for dtype '{self.dtype}'\"\n            ) from err\n        except NotImplementedError:\n            # np_can_hold_element doesn't handle all dtypes (e.g. \"U\"),\n            # fall back to no validation for those.\n            return value\n\n    def searchsorted(\n        self,\n        value: NumpyValueArrayLike | ExtensionArray,\n        side: Literal[\"left\", \"right\"] = \"left\",\n        sorter: NumpySorter | None = None,\n    ) -> npt.NDArray[np.intp] | np.intp:\n        # Parent's searchsorted calls _validate_setitem_value, which is\n        # too strict for search (e.g. rejects float into int). Delegate\n        # directly to numpy which handles cross-dtype searches correctly.\n        return self._ndarray.searchsorted(value, side=side, sorter=sorter)  # type: ignore[arg-type]\n","sourceCodeStart":167,"sourceCodeEnd":203,"githubUrl":"https://github.com/pandas-dev/pandas/blob/3b7651241d4da534b3559b60ef128e1c34f54116/pandas/core/arrays/numpy_.py#L167-L203","documentation":"Raised by NumpyExtensionArray._validate_setitem_value when np_can_hold_element raises LossySetitemError — i.e. the value cannot be stored in the array's numpy dtype without loss of information (e.g. putting 1.5 into an int64 array, or a large int into int8). The original LossySetitemError is chained as the cause. This guards item assignment (arr[i] = v) and fill operations so silent truncation cannot occur.","triggerScenarios":"arr = pd.array([1, 2, 3]); arr[0] = 1.5 — float into Int64. arr[0] = 10**20 into an Int8 column. Filling an integer NumpyExtensionArray with a float value via where/fillna that routes through _validate_setitem_value.","commonSituations":"Conditional assignment with a value whose type doesn't fit; merging/joining columns of narrower dtype; user expects silent numpy-style truncation but pandas now validates.","solutions":["Cast the value to the array's dtype before assignment: int(value), or value.astype(arr.dtype.numpy_dtype).","Widen the array dtype to fit the value: arr = arr.astype('Int64') (or Float64) before assignment.","Use pd.array(..., dtype=<wider>) at construction so the value is representable."],"exampleFix":"# before\narr = pd.array([1, 2, 3], dtype='Int8')\narr[0] = 200  # raises (200 > 127)\n\n# after\narr = arr.astype('Int16')\narr[0] = 200","handlingStrategy":"try-catch","validationCode":"import numpy as np\n\ndef safe_setitem(arr, idx, value):\n    np_dtype = arr.dtype.numpy_dtype if hasattr(arr.dtype, 'numpy_dtype') else arr.dtype\n    try:\n        value = np.array(value).astype(np_dtype).item()\n    except (TypeError, OverflowError, ValueError):\n        arr = arr.astype('Float64')\n    arr[idx] = value\n    return arr","typeGuard":"import numpy as np\n\ndef value_fits_dtype(value, np_dtype) -> bool:\n    try:\n        np.array(value).astype(np_dtype)\n        return True\n    except (TypeError, OverflowError, ValueError):\n        return False","tryCatchPattern":"try:\n    arr[i] = value\nexcept TypeError as e:\n    if 'Invalid value' in str(e):\n        arr = arr.astype('Float64')\n        arr[i] = value\n    else:\n        raise","preventionTips":["Cast incoming values to the column's numpy dtype before assignment.","When values may overflow, widen the dtype proactively (Int8 -> Int16/Int32/Int64).","Validate ranges in your ingest layer rather than relying on assignment to fail."],"tags":["pandas","numpy-extension-array","setitem","casting","lossy"],"backgroundTag":null,"analyzedSha":"3b7651241d4da534b3559b60ef128e1c34f54116","analyzedAt":"2026-08-11T22:10:44.015Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}