Check elementwise if an Interval overlaps the values in the IntervalArray in Python Pandas

To check elementwise if an Interval overlaps the values in the IntervalArray, use the overlaps() method in Pandas. Two intervals overlap if they share a common point, including closed endpoints. Intervals that only have an open endpoint in common do not overlap.

Syntax

IntervalArray.overlaps(other)

Parameters

other ? An Interval object to check overlap against each interval in the array.

Creating an IntervalArray

First, let's create an IntervalArray from tuples ?

import pandas as pd

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

# Display the IntervalArray
print("IntervalArray...")
print(intervals)
IntervalArray...
<IntervalArray>
[(10, 20], (15, 35]]
Length: 2, dtype: interval[int64, right]

Checking Overlap with an Interval

Now check elementwise if an Interval overlaps the values in the IntervalArray ?

import pandas as pd

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

# Check elementwise if an Interval overlaps the values in the IntervalArray
result = intervals.overlaps(pd.Interval(12, 30))
print("Does interval (12, 30] overlap with each interval?")
print(result)
Does interval (12, 30] overlap with each interval?
[ True  True]

How It Works

The overlaps() method compares the given interval with each interval in the array:

  • Interval (12, 30] overlaps with (10, 20] because they share the range [12, 20]
  • Interval (12, 30] overlaps with (15, 35] because they share the range [15, 30]

Example with Non-overlapping Interval

Let's test with an interval that doesn't overlap ?

import pandas as pd

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

# Check with a non-overlapping interval
result = intervals.overlaps(pd.Interval(25, 40))
print("Does interval (25, 40] overlap with each interval?")
print(result)
Does interval (25, 40] overlap with each interval?
[False  True]

Conclusion

The overlaps() method returns a boolean array indicating whether each interval in the IntervalArray overlaps with the specified interval. It's useful for filtering and analyzing interval data in time series or range-based applications.

Updated on: 2026-03-26T17:19:24+05:30

382 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements