{"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":"validation","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/3b7651241d4da534b3559b60ef128e1c34f54116/pandas/core/arrays/interval.py#L1165-L1201","documentation":"Raised in IntervalArray._validate_scalar when the scalar value is neither a pandas.Interval nor a valid NA for the dtype. Scalars inserted into an IntervalArray must be Interval objects or recognized NA.","triggerScenarios":"arr.insert(0, 5); arr.insert(0, 'x'); _validate_scalar(3.14) on an IntervalArray; assigning an unrecognized sentinel.","commonSituations":"Inserting raw scalars; using non-standard NA values not recognized for the dtype; building rows programmatically.","solutions":["Wrap the scalar as pd.Interval(left, right, closed=arr.closed).","Use pd.NA (or np.nan where the subtype allows) for missing.","Ensure the Interval's closed matches the array's closed."],"exampleFix":"// before\narr.insert(0, 5)\n// after\narr.insert(0, pd.Interval(0, 5, closed=arr.closed))","handlingStrategy":"type-guard","validationCode":"import pandas as pd\n\ndef as_interval_scalar(v, closed):\n    if v is pd.NA:\n        return pd.NA\n    if isinstance(v, pd.Interval):\n        return v\n    raise TypeError('expected pandas.Interval or pd.NA')","typeGuard":"import pandas as pd\n\ndef is_interval_or_na(v):\n    return v is pd.NA or isinstance(v, pd.Interval)","tryCatchPattern":null,"preventionTips":["Always pass pd.Interval(...) or pd.NA to insert and scalar setitem.","Match the Interval's closed to the target array's closed.","Avoid passing raw scalars or strings into interval arrays."],"tags":["interval-array","insert","scalar","validation"],"backgroundTag":null,"analyzedSha":"3b7651241d4da534b3559b60ef128e1c34f54116","analyzedAt":"2026-08-11T22:10:44.015Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}