Python Pandas IntervalArray - Check Intervals that only have an open endpoint in common overlap or not

To check if intervals that only have an open endpoint in common overlap or not, use the overlaps() method with the closed parameter in Pandas IntervalArray.

Understanding Interval Overlap

Two intervals overlap if they share a common point, including closed endpoints. However, intervals with only an open endpoint in common (like (10, 20) and (20, 30) where 20 is open in the first interval) may or may not overlap depending on the closed parameter.

Creating an IntervalArray

First, let's create an IntervalArray with intervals that share an endpoint ?

import pandas as pd

# Create an IntervalArray with intervals sharing endpoint 20
intervals = pd.arrays.IntervalArray.from_tuples([(10, 20), (20, 35)])

print("IntervalArray...")
print(intervals)
print("\nInterval length...")
print(intervals.length)
IntervalArray...
<IntervalArray>
[(10, 20], (20, 35]]
Length: 2, dtype: interval[int64, right]

Interval length...
Int64Index([10, 15], dtype='int64')

Checking Overlap with Open Endpoint

Now let's check if these intervals overlap with another interval that has a closed right endpoint ?

import pandas as pd

intervals = pd.arrays.IntervalArray.from_tuples([(10, 20), (20, 35)])

# Check overlap with interval (20, 25] - closed on right side
test_interval = pd.Interval(20, 25, closed='right')
overlap_result = intervals.overlaps(test_interval)

print("IntervalArray:")
print(intervals)
print(f"\nTest interval: {test_interval}")
print(f"\nOverlap results: {overlap_result}")
IntervalArray:
<IntervalArray>
[(10, 20], (20, 35]]
Length: 2, dtype: interval[int64, right]

Test interval: (20.0, 25.0]

Overlap results: [False  True]

How It Works

The overlaps() method returns a boolean array indicating which intervals overlap with the test interval:

  • First interval (10, 20]: Returns False because it ends at 20 (closed) but the test interval starts at 20 (open), so they only share an open endpoint
  • Second interval (20, 35]: Returns True because it overlaps with (20, 25] from 20 to 25

Different Closed Parameters

Let's see how different closed values affect the overlap detection ?

import pandas as pd

intervals = pd.arrays.IntervalArray.from_tuples([(10, 20), (20, 35)])

# Test with different closed parameters
test_both = pd.Interval(20, 25, closed='both')
test_left = pd.Interval(20, 25, closed='left')
test_neither = pd.Interval(20, 25, closed='neither')

print("Original intervals:", intervals)
print(f"Closed='both' [20, 25]: {intervals.overlaps(test_both)}")
print(f"Closed='left' [20, 25): {intervals.overlaps(test_left)}")
print(f"Closed='neither' (20, 25): {intervals.overlaps(test_neither)}")
Original intervals: <IntervalArray>
[(10, 20], (20, 35]]
Length: 2, dtype: interval[int64, right]
Closed='both' [20, 25]: [ True  True]
Closed='left' [20, 25): [ True  True]
Closed='neither' (20, 25): [False  True]

Conclusion

The overlaps() method with different closed parameters allows precise control over interval overlap detection. Intervals with only open endpoints in common will not overlap, while those with at least one closed shared endpoint will overlap.

---
Updated on: 2026-03-26T17:20:02+05:30

173 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements