Python Pandas - Return an IntervalArray identical to the current one but closed on the left side

To return an IntervalArray identical to the current one but closed on the left side, use the set_closed() method with value left. This method allows you to change the closure type of intervals without modifying the actual interval boundaries.

Understanding Interval Closure

An interval can be closed on different sides:

  • right (default): (a, b] includes b but excludes a
  • left: [a, b) includes a but excludes b
  • both: [a, b] includes both a and b
  • neither: (a, b) excludes both a and b

Creating an IntervalArray

First, let's create an IntervalArray using from_breaks() ?

import pandas as pd

# Create IntervalArray from breaks
index = pd.arrays.IntervalArray.from_breaks(range(5))
print("Original IntervalArray:")
print(index)
print(f"Closure type: {index.closed}")
Original IntervalArray:
<IntervalArray>
[(0, 1], (1, 2], (2, 3], (3, 4]]
Length: 4, dtype: interval[int64, right]
Closure type: right

Changing Closure to Left

Use set_closed('left') to change the closure type ?

import pandas as pd

# Create IntervalArray
index = pd.arrays.IntervalArray.from_breaks(range(5))

# Change closure to left
left_closed = index.set_closed('left')

print("Original (right-closed):")
print(index)
print(f"Closure: {index.closed}")

print("\nAfter set_closed('left'):")
print(left_closed)
print(f"Closure: {left_closed.closed}")
Original (right-closed):
<IntervalArray>
[(0, 1], (1, 2], (2, 3], (3, 4]]
Length: 4, dtype: interval[int64, right]
Closure: right

After set_closed('left'):
<IntervalArray>
[[0, 1), [1, 2), [2, 3), [3, 4)]
Length: 4, dtype: interval[int64, left]
Closure: left

Complete Example

Here's a comprehensive example showing interval properties and closure change ?

import pandas as pd

# Create IntervalArray
index = pd.arrays.IntervalArray.from_breaks(range(5))

# Display original interval
print("IntervalArray:")
print(index)

# Display interval properties
print(f"\nInterval length: {list(index.length)}")
print(f"Left bounds: {list(index.left)}")
print(f"Right bounds: {list(index.right)}")

# Change to left-closed
result = index.set_closed('left')
print(f"\nLeft-closed intervals:")
print(result)
IntervalArray:
<IntervalArray>
[(0, 1], (1, 2], (2, 3], (3, 4]]
Length: 4, dtype: interval[int64, right]

Interval length: [1, 1, 1, 1]
Left bounds: [0, 1, 2, 3]
Right bounds: [1, 2, 3, 4]

Left-closed intervals:
<IntervalArray>
[[0, 1), [1, 2), [2, 3), [3, 4)]
Length: 4, dtype: interval[int64, left]

Other Closure Options

The set_closed() method accepts four closure types ?

import pandas as pd

index = pd.arrays.IntervalArray.from_breaks([0, 2, 4])

print("Original:", index)
print("Left:", index.set_closed('left'))
print("Right:", index.set_closed('right'))  
print("Both:", index.set_closed('both'))
print("Neither:", index.set_closed('neither'))
Original: <IntervalArray>
[(0, 2], (2, 4]]
Length: 2, dtype: interval[int64, right]
Left: <IntervalArray>
[[0, 2), [2, 4)]
Length: 2, dtype: interval[int64, left]
Right: <IntervalArray>
[(0, 2], (2, 4]]
Length: 2, dtype: interval[int64, right]
Both: <IntervalArray>
[[0, 2], [2, 4]]
Length: 2, dtype: interval[int64, both]
Neither: <IntervalArray>
[(0, 2), (2, 4)]
Length: 2, dtype: interval[int64, neither]

Conclusion

The set_closed('left') method returns a new IntervalArray with left-closed intervals, changing the notation from (a, b] to [a, b). This is useful for adjusting interval boundaries to match specific mathematical or analytical requirements.

Updated on: 2026-03-26T17:00:42+05:30

156 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements