Python Pandas - Create an IntervalArray from an array of splits and check the intervals are closed on the left or right-side, both or neither

To create an IntervalArray from an array of splits, use pandas.arrays.IntervalArray.from_breaks(). To check whether the intervals are closed on the left or right-side, both or neither, use the closed property.

Creating IntervalArray from Breaks

The from_breaks() method constructs intervals from an array of split points. By default, intervals are closed on the right side ?

import pandas as pd

# Create IntervalArray from breaks (closed on right by default)
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]

Checking Closure Type

Use the closed property to determine how intervals are closed ?

import pandas as pd

array = pd.arrays.IntervalArray.from_breaks([0, 1, 2, 3, 4, 5])
print("Interval closure type:", array.closed)
Interval closure type: right

Different Closure Types

You can specify different closure types using the closed parameter ?

import pandas as pd

# Left-closed intervals
left_closed = pd.arrays.IntervalArray.from_breaks([0, 1, 2, 3], closed='left')
print("Left-closed intervals:")
print(left_closed)
print("Closure type:", left_closed.closed)

print("\n" + "="*40 + "\n")

# Both-closed intervals
both_closed = pd.arrays.IntervalArray.from_breaks([0, 1, 2, 3], closed='both')
print("Both-closed intervals:")
print(both_closed)
print("Closure type:", both_closed.closed)
Left-closed intervals:
<IntervalArray>
[[0, 1), [1, 2), [2, 3)]
Length: 3, dtype: interval[int64, left]
Closure type: left

========================================

Both-closed intervals:
<IntervalArray>
[[0, 1], [1, 2], [2, 3]]
Length: 3, dtype: interval[int64, both]
Closure type: both

Complete Example

Here's a comprehensive example showing IntervalArray properties ?

import pandas as pd

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

print("IntervalArray:")
print(array)

print(f"\nClosure type: {array.closed}")
print(f"Length of each interval: {array.length}")
print(f"Midpoint of each interval: {array.mid}")
print(f"Right endpoints: {array.right}")
print(f"Left endpoints: {array.left}")
IntervalArray:
<IntervalArray>
[(0, 1], (1, 2], (2, 3], (3, 4], (4, 5]]
Length: 5, dtype: interval[int64, right]

Closure type: right
Length of each interval: [1 1 1 1 1]
Midpoint of each interval: [0.5 1.5 2.5 3.5 4.5]
Right endpoints: [1 2 3 4 5]
Left endpoints: [0 1 2 3 4]

Closure Types Summary

Closure Type Notation Description
'right' (a, b] Closed on right, open on left
'left' [a, b) Closed on left, open on right
'both' [a, b] Closed on both sides
'neither' (a, b) Open on both sides

Conclusion

Use IntervalArray.from_breaks() to create intervals from split points. The closed property shows whether intervals are closed on the left, right, both sides, or neither.

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

309 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements