{"record":{"id":"436ccf8bb5a548f3","repo":"pandas-dev/pandas","slug":"cannot-set-float-nan-to-integer-backed-intervalarr","errorCode":null,"errorMessage":"Cannot set float NaN to integer-backed IntervalArray","messagePattern":"Cannot set float NaN to integer-backed IntervalArray","errorType":"exception","errorClass":"TypeError","httpStatus":null,"severity":"error","filePath":"pandas/core/arrays/interval.py","lineNumber":1199,"sourceCode":"            left = right = self.left._na_value\n        else:\n            raise TypeError(\n                \"can only insert Interval objects and NA into an IntervalArray\"\n            )\n        return left, right\n\n    def _validate_setitem_value(self, value):\n        if is_list_like(value):\n            return self._validate_listlike(value)\n\n        left, right = self._validate_scalar(value)\n\n        if is_valid_na_for_dtype(value, self.left.dtype):\n            if is_integer_dtype(self.dtype.subtype):\n                # can't set NaN on a numpy integer array\n                # GH#45484 TypeError, not ValueError, matches what we get with\n                #  non-NA un-holdable value.\n                raise TypeError(\"Cannot set float NaN to integer-backed IntervalArray\")\n\n        return left, right\n\n    # ---------------------------------------------------------------------\n    # Rendering Methods\n\n    def _formatter(self, boxed: bool = False) -> Callable[[object], str]:\n        # returning 'str' here causes us to render as e.g. \"(0, 1]\" instead of\n        #  \"Interval(0, 1, closed='right')\"\n        return str\n\n    # ---------------------------------------------------------------------\n    # Vectorized Interval Properties/Attributes\n\n    @property\n    def left(self) -> Index:\n        \"\"\"\n        Return the left endpoints of each Interval in the IntervalArray as an Index.","sourceCodeStart":1181,"sourceCodeEnd":1217,"githubUrl":"https://github.com/pandas-dev/pandas/blob/71959b8cb9b2459c16e14b34f28b178ccfe14735/pandas/core/arrays/interval.py#L1181-L1217","documentation":"Raised by IntervalArray._validate_setitem_value when the scalar is a valid NA for the dtype but the underlying endpoint array is numpy integer-backed, which cannot store NaN. Pandas raises TypeError (not ValueError) intentionally to match the lossy-setitem contract for non-NA values on int arrays (GH#45484). Use a nullable subtype to allow NA.","triggerScenarios":"Assigning np.nan or pd.NA into an IntervalArray with dtype 'interval[int64]', e.g. arr[i] = np.nan where arr.dtype.subtype is int64.","commonSituations":"Migrating code that worked on float-backed intervals to int-backed intervals; cleaning data pipelines that impute NaN into interval columns; reading parquet/CSV that yielded int intervals and then trying to mask out values.","solutions":["Cast the array to a nullable-integer subtype: arr = arr.astype('interval[Int64]') or 'interval[float64]' before assigning NaN.","Drop the row/index rather than setting NA if the subtype must stay numpy int.","Build the IntervalArray with a nullable dtype from the start via pd.array(..., dtype='interval[Int64]')."],"exampleFix":"# before\narr = pd.arrays.IntervalArray.from_tuples([(0, 1), (2, 3)])\narr[0] = np.nan\n\n# after\narr = arr.astype('interval[float64]')\narr[0] = np.nan","handlingStrategy":"validation","validationCode":"def can_hold_na(arr) -> bool:\n    return not (np.issubdtype(arr.dtype.subtype, np.integer) and\n                not isinstance(arr.dtype.subtype, pd.api.types.pandas_dtype('Int64').type))\n\n# simpler: check the dtype string\ndef needs_nullable_for_na(arr):\n    return 'int' in str(arr.dtype.subtype).lower() and 'Int' not in str(arr.dtype.subtype)","typeGuard":"def is_nullable_interval(arr) -> bool:\n    sub = str(arr.dtype.subtype)\n    return sub.startswith('Int') or sub.startswith('float') or sub.startswith('datetime')","tryCatchPattern":"try:\n    arr[i] = np.nan\nexcept TypeError as e:\n    if 'integer-backed IntervalArray' in str(e):\n        arr = arr.astype('interval[float64]')\n        arr[i] = np.nan","preventionTips":["Build interval columns with nullable subtypes ('Int64','Float64') if NA is possible.","Drop rows instead of masking when subtype must be numpy int.","Document NA support per interval column in your schema."],"tags":["interval-array","dtype","missing-data","setitem"],"analyzedSha":"71959b8cb9b2459c16e14b34f28b178ccfe14735","analyzedAt":"2026-08-07T01:30:20.476Z","schemaVersion":2},"datasetVersion":"2026-08-07T03:17:09.362Z"}