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

PythonServer Side ProgrammingProgramming

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]
raja
Updated on 13-Oct-2021 11:05:58

Advertisements