{"record":{"id":"93de93a453f1d067","repo":"pandas-dev/pandas","slug":"can-only-insert-interval-objects-and-na-into-an-in","errorCode":null,"errorMessage":"can only insert Interval objects and NA into an IntervalArray","messagePattern":"can only insert Interval objects and NA into an IntervalArray","errorType":"exception","errorClass":"TypeError","httpStatus":null,"severity":"error","filePath":"pandas/core/arrays/interval.py","lineNumber":1183,"sourceCode":"            msg = (\n                \"'value' should be a compatible interval type, \"\n                f\"got {type(value)} instead.\"\n            )\n            raise TypeError(msg) from err\n\n        return value_left, value_right\n\n    def _validate_scalar(self, value):\n        if isinstance(value, Interval):\n            self._check_closed_matches(value, name=\"value\")\n            left, right = value.left, value.right\n            self.left._validate_fill_value(left)\n            self.left._validate_fill_value(right)\n        elif is_valid_na_for_dtype(value, self.left.dtype):\n            # GH#18295\n            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","sourceCodeStart":1165,"sourceCodeEnd":1201,"githubUrl":"https://github.com/pandas-dev/pandas/blob/71959b8cb9b2459c16e14b34f28b178ccfe14735/pandas/core/arrays/interval.py#L1165-L1201","documentation":"Raised by IntervalArray._validate_scalar when a scalar value passed to insert/fill/shift is neither a pd.Interval nor a recognised NA (is_valid_na_for_dtype returns False). IntervalArray only accepts Interval objects or NA as scalar payloads. The guard is reached by insert(), _validate_setitem_value scalar path, and shift(fill_value=...).","triggerScenarios":"Calling arr.insert(loc, 5) on an IntervalArray, arr.fillna(0), or shift(fill_value=-1) where the fill is a plain scalar rather than an Interval or NA.","commonSituations":"Treating an IntervalArray like a numeric array and trying to insert a single number, or assuming 0 / '' is a safe fill.","solutions":["Pass a pd.Interval(left, right, closed=arr.closed) as the scalar to insert().","Pass pd.NA (or np.nan for float-backed) when the intent is a missing value.","If you need a numeric value, switch to the underlying endpoints via arr.left / arr.right rather than the interval array."],"exampleFix":"# before\narr = pd.arrays.IntervalArray.from_tuples([(0, 1), (2, 3)])\narr.insert(1, 5)\n\n# after\narr.insert(1, pd.Interval(5, 6, closed=arr.closed))","handlingStrategy":"type-guard","validationCode":"def interval_or_na(value, closed):\n    if value is pd.NA or value is None or (isinstance(value, float) and np.isnan(value)):\n        return pd.NA\n    if isinstance(value, pd.Interval):\n        if value.closed != closed:\n            raise ValueError('closed mismatch')\n        return value\n    raise TypeError('pass an Interval or pd.NA')","typeGuard":"def is_interval_or_na(value) -> bool:\n    return isinstance(value, pd.Interval) or value is pd.NA or value is None","tryCatchPattern":null,"preventionTips":["Use pd.Interval explicitly for scalar inserts.","Default fill_value to pd.NA, never to 0 or ''.","Unit-test insert/fill paths against non-interval scalars."],"tags":["interval-array","type-error","insert"],"analyzedSha":"71959b8cb9b2459c16e14b34f28b178ccfe14735","analyzedAt":"2026-08-07T01:30:20.476Z","schemaVersion":2},"datasetVersion":"2026-08-07T03:17:09.362Z"}