pandas-dev/pandas · error · NotImplementedError

contains not implemented for two intervals

Error message

contains not implemented for two intervals

What it means

Raised in IntervalArray.contains when 'other' is a pandas.Interval. The vectorized 'contains' method only checks scalar points against each interval; interval-vs-interval containment is not implemented and is explicitly rejected.

Solutions

  1. Use arr.overlaps(other) if intersection semantics are wanted.
  2. Implement containment manually: (arr.left <= other.left) & (other.right <= arr.right).
  3. Iterate per-element for nested containment checks.

Example fix

// before
arr.contains(pd.Interval(1, 2))
// after
(arr.left <= 1) & (arr.right >= 2)  # bound-wise containment check
Defensive patterns

Strategy: fallback

Validate before calling

def contains_interval(arr, other):
    # other is a pandas.Interval
    return (arr.left <= other.left) & (other.right <= arr.right)

Type guard

import pandas as pd

def is_scalar_point(v):
    return not isinstance(v, pd.Interval)

Prevention

When it happens

Trigger: arr.contains(pd.Interval(1, 2)); passing an Interval to contains expecting nested-containment semantics.

Common situations: Confusing contains with overlaps; attempting to test whether intervals nest or contain one another.

Related errors


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

Appendix: source

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

        See Also
        --------
        Interval.contains : Check whether Interval object contains value.
        IntervalArray.overlaps : Check if an Interval overlaps the values in the
            IntervalArray.

        Examples
        --------
        >>> intervals = pd.arrays.IntervalArray.from_tuples([(0, 1), (1, 3), (2, 4)])
        >>> intervals
        <IntervalArray>
        [(0, 1], (1, 3], (2, 4]]
        Length: 3, dtype: interval[int64, right]

        >>> intervals.contains(0.5)
        array([ True, False, False])
        """
        if isinstance(other, Interval):
            raise NotImplementedError("contains not implemented for two intervals")

        return (self._left < other if self.open_left else self._left <= other) & (
            other < self._right if self.open_right else other <= self._right
        )

    def isin(self, values: ArrayLike) -> npt.NDArray[np.bool_]:
        if isinstance(values, IntervalArray):
            if self.closed != values.closed:
                # not comparable -> no overlap
                return np.zeros(self.shape, dtype=bool)

            if self.dtype == values.dtype:
                left = self._combined
                right = values._combined
                return np.isin(left, right).ravel()

            elif needs_i8_conversion(self.left.dtype) ^ needs_i8_conversion(
                values.left.dtype

View on GitHub (pinned to 3b7651241d)