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.

At first, import the required libraries −

import pandas as pd

Create IntervalArray −

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

Display the interval −

print("IntervalIndex...\n",index)

Return an IntervalArray identical to the current one but closed on specified side i.e. "left" here −

print("\nResult...",index.set_closed('left'))

Example

Following is the code −

import pandas as pd

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

# Display the interval
print("IntervalIndex...\n",index)

# Display the interval length
print("\nIntervalIndex length...\n",index.length)

# the left bound
print("\nThe left bound for the IntervalIndex...\n",index.left)

# the right bound
print("\nThe right bound for the IntervalIndex...\n",index.right)

# Return an IntervalArray identical to the current one but closed on specified
# side i.e. "left" here
print("\nResult...",index.set_closed('left'))

Output

This will produce the following output −

IntervalIndex...
<IntervalArray>
[(0, 1], (1, 2], (2, 3], (3, 4]]
Length: 4, dtype: interval[int64, right]

IntervalIndex length...
Int64Index([1, 1, 1, 1], dtype='int64')

The left bound for the IntervalIndex...
Int64Index([0, 1, 2, 3], dtype='int64')

The right bound for the IntervalIndex...
Int64Index([1, 2, 3, 4], dtype='int64')

Result... <IntervalArray>
[[0, 1), [1, 2), [2, 3), [3, 4)]
Length: 4, dtype: interval[int64, left]

Updated on: 18-Oct-2021

78 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements