pandas-dev/pandas · error · TypeError

'value' should be a compatible interval type, got {type(valu

Error message

'value' should be a compatible interval type, got {type(value)} instead.

What it means

Raised after the value is successfully parsed as an IntervalArray but its endpoint dtype is rejected by self.left._validate_fill_value (raises LossySetitemError or TypeError). This means the value is interval-shaped but its subtype is incompatible with the target's subtype, e.g. assigning datetime intervals into a numeric-backed IntervalArray. The error distinguishes 'shape ok, dtype wrong' from error 321 ('shape wrong').

Source

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

    def _validate_listlike(self, value):
        # list-like of intervals
        try:
            array = IntervalArray(value)
            self._check_closed_matches(array, name="value")
            value_left, value_right = array.left, array.right
        except TypeError as err:
            # wrong type: not interval or NA
            msg = f"'value' should be an interval type, got {type(value)} instead."
            raise TypeError(msg) from err

        try:
            self.left._validate_fill_value(value_left)
        except (LossySetitemError, TypeError) as err:
            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

View on GitHub (pinned to 71959b8cb9)

Solutions

  1. Cast the target array to a wider subtype first: arr = arr.astype('interval[float64]') before assignment.
  2. Rebuild the value with endpoints matching arr.dtype.subtype, e.g. pd.Interval(int(left), int(right)).
  3. Confirm closed matches too via arr._check_closed_matches(value) before assigning.

Example fix

# before
arr = pd.arrays.IntervalArray.from_tuples([(0, 1)], dtype='interval[int64]')
arr[0] = pd.Interval(0.5, 1.5)

# after
arr = arr.astype('interval[float64]')
arr[0] = pd.Interval(0.5, 1.5)
Defensive patterns

Strategy: validation

Validate before calling

def compatible_value(arr, value):
    iv = value if isinstance(value, pd.Interval) else pd.arrays.IntervalArray(value)
    sub = iv.left.dtype if isinstance(iv, pd.arrays.IntervalArray) else type(iv.left)
    if not np.can_cast(sub, arr.dtype.subtype):
        raise TypeError(f'value subtype {sub} incompatible with {arr.dtype.subtype}')
    return iv

Type guard

def endpoints_compatible(arr, value) -> bool:
    try:
        arr.left._validate_fill_value(getattr(value, 'left', value))
        return True
    except (TypeError, Exception):
        return False

Try / catch

try:
    arr[i] = value
except TypeError as e:
    if 'compatible interval type' in str(e):
        arr = arr.astype('interval[float64]')
        arr[i] = value

Prevention

When it happens

Trigger: Assigning intervals whose endpoints are floats into an int64-backed IntervalArray with values that would truncate, or assigning Timestamp-backed intervals into a numeric interval array, via arr[i] = other_array.

Common situations: Mixing interval arrays created with different subtypes (int vs float vs datetime), merging interval columns from heterogeneous sources, or attempting to widen/narrow precision during assignment.

Related errors


AI-assisted analysis of pandas-dev/pandas@71959b8cb9 (2026-08-07). Data as JSON: /api/errors/99cbf9a95bdec9e4. Report an issue: GitHub.