pandas-dev/pandas · error · TypeError

can only insert Interval objects and NA into an…

Error message

can only insert Interval objects and NA into an IntervalArray

What it means

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.

Solutions

  1. Wrap the scalar as pd.Interval(left, right, closed=arr.closed).
  2. Use pd.NA (or np.nan where the subtype allows) for missing.
  3. Ensure the Interval's closed matches the array's closed.

Example fix

// before
arr.insert(0, 5)
// after
arr.insert(0, pd.Interval(0, 5, closed=arr.closed))
Defensive patterns

Strategy: type-guard

Validate before calling

import pandas as pd

def as_interval_scalar(v, closed):
    if v is pd.NA:
        return pd.NA
    if isinstance(v, pd.Interval):
        return v
    raise TypeError('expected pandas.Interval or pd.NA')

Type guard

import pandas as pd

def is_interval_or_na(v):
    return v is pd.NA or isinstance(v, pd.Interval)

Prevention

When it happens

Trigger: arr.insert(0, 5); arr.insert(0, 'x'); _validate_scalar(3.14) on an IntervalArray; assigning an unrecognized sentinel.

Common situations: Inserting raw scalars; using non-standard NA values not recognized for the dtype; building rows programmatically.

Related errors


AI-assisted analysis of pandas-dev/pandas@3b7651241d (2026-08-11). Data as JSON: /api/errors/93de93a453f1d067. Report an issue: GitHub.

Appendix: source

Thrown at pandas/core/arrays/interval.py:1183

            msg = (
                "'value' should be a compatible interval type, "
                f"got {type(value)} instead."
            )
            raise TypeError(msg) from err

        return value_left, value_right

    def _validate_scalar(self, value):
        if isinstance(value, Interval):
            self._check_closed_matches(value, name="value")
            left, right = value.left, value.right
            self.left._validate_fill_value(left)
            self.left._validate_fill_value(right)
        elif is_valid_na_for_dtype(value, self.left.dtype):
            # GH#18295
            left = right = self.left._na_value
        else:
            raise TypeError(
                "can only insert Interval objects and NA into an IntervalArray"
            )
        return left, right

    def _validate_setitem_value(self, value):
        if is_list_like(value):
            return self._validate_listlike(value)

        left, right = self._validate_scalar(value)

        if is_valid_na_for_dtype(value, self.left.dtype):
            if is_integer_dtype(self.dtype.subtype):
                # can't set NaN on a numpy integer array
                # GH#45484 TypeError, not ValueError, matches what we get with
                #  non-NA un-holdable value.
                raise TypeError("Cannot set float NaN to integer-backed IntervalArray")

        return left, right

View on GitHub (pinned to 3b7651241d)