Return IntervalArray Closed on Specified Side in Python Pandas



To return an IntervalArray identical to the current one but closed on the specified side, use the array.set_closed() with parameter both.

At first, import the required libraries −

import pandas as pd

Construct a new IntervalArray from an array-like of splits −

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

Display the intervals −

print("Our IntervalArray...\n",array)

An IntervalArray identical to the current one but closed on the specified side −

print("\nAn identical IntervalArray closed on the specified side...\n", array.set_closed('both'))

Example

Following is the code −

import pandas as pd

# Construct a new IntervalArray from an array-like of splits
array = pd.arrays.IntervalArray.from_breaks([0, 1, 2, 3, 4, 5])

# Display the IntervalArray
print("Our IntervalArray...\n",array)

print("\nAn identical IntervalArray closed on the specified side...\n", array.set_closed('both'))

Output

This will produce the following code −

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

An identical IntervalArray closed on the specified side...
<IntervalArray>
[[0, 1], [1, 2], [2, 3], [3, 4], [4, 5]]
Length: 5, dtype: interval[int64, both]
Updated on: 2021-10-13T11:05:58+05:30

95 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements