Python Pandas - Check elementwise if the Intervals contain the value

To check elementwise if the Intervals contain a specific value, use the contains() method on a Pandas IntervalArray. This method returns a boolean array indicating which intervals contain the given value.

Creating an IntervalArray

First, let's create an IntervalArray from break points ?

import pandas as pd

# Create IntervalArray from break points
array = pd.arrays.IntervalArray.from_breaks([0, 1, 2, 3, 4, 5])
print("Our IntervalArray:")
print(array)
Our IntervalArray:
<IntervalArray>
[(0, 1], (1, 2], (2, 3], (3, 4], (4, 5]]
Length: 5, dtype: interval[int64, right]

Using contains() Method

The contains() method checks each interval to see if it contains the specified value ?

import pandas as pd

array = pd.arrays.IntervalArray.from_breaks([0, 1, 2, 3, 4, 5])

# Check if intervals contain the value 3.5
result = array.contains(3.5)
print("Does each interval contain 3.5?")
print(result)

# Check with different values
print("\nContains 1.0:")
print(array.contains(1.0))

print("\nContains 0.5:")
print(array.contains(0.5))
Does each interval contain 3.5?
[False False False  True False]

Contains 1.0:
[ True False False False False]

Contains 0.5:
[ True False False False False]

Understanding Interval Boundaries

By default, intervals are right-closed, meaning they include the right endpoint but exclude the left endpoint ?

import pandas as pd

array = pd.arrays.IntervalArray.from_breaks([0, 1, 2, 3, 4, 5])

print("Interval properties:")
print("Left endpoints:", array.left.tolist())
print("Right endpoints:", array.right.tolist())
print("Midpoints:", array.mid.tolist())

# Test boundary values
print("\nBoundary tests:")
print("Contains 1 (right boundary of first interval):", array.contains(1))
print("Contains 0 (left boundary of first interval):", array.contains(0))
Interval properties:
Left endpoints: [0, 1, 2, 3, 4]
Right endpoints: [1, 2, 3, 4, 5]
Midpoints: [0.5, 1.5, 2.5, 3.5, 4.5]

Boundary tests:
Contains 1 (right boundary of first interval): [ True False False False False]
Contains 0 (left boundary of first interval): [False False False False False]

Practical Example with Real Data

Here's how you might use this in practice to categorize values ?

import pandas as pd

# Create grade intervals
grades = pd.arrays.IntervalArray.from_breaks([0, 60, 70, 80, 90, 100])
print("Grade intervals:")
print(grades)

# Check which grade range a score falls into
test_scores = [55, 75, 85, 95]

for score in test_scores:
    contains_result = grades.contains(score)
    interval_index = contains_result.argmax() if contains_result.any() else -1
    print(f"Score {score}: {grades[interval_index] if interval_index != -1 else 'No interval'}")
Grade intervals:
<IntervalArray>
[(0, 60], (60, 70], (70, 80], (80, 90], (90, 100]]
Length: 5, dtype: interval[int64, right]

Score 55: (0, 60]
Score 75: (70, 80]
Score 85: (80, 90]
Score 95: (90, 100]

Conclusion

The contains() method provides an efficient way to check which intervals contain a specific value, returning a boolean array for elementwise comparison. This is particularly useful for data categorization and range-based filtering operations.

Updated on: 2026-03-26T16:11:40+05:30

673 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements