Python Pandas IntervalIndex - Check if an interval that contains points is empty or not

PythonServer Side ProgrammingProgramming

To check if an interval that contains points is empty or not, use the IntervalIndex.is_empty property in Pandas.

At first, import the required libraries −

import pandas as pd

Create IntervalIndex −

interval = pd.IntervalIndex.from_arrays([0, 1], [1, 2], closed='right')

Display the interval −

print("IntervalIndex...
",interval)

Check if the interval that contains points is empty or not −

print("
Is the interval empty?
",interval.is_empty)

Example

Following is the code −

import pandas as pd

# Create IntervalIndex
interval = pd.IntervalIndex.from_arrays([0, 1], [1, 2], closed='right')

# Display the interval
print("IntervalIndex...
",interval) # Display the interval length print("
IntervalIndex length...
",interval.length) # check if the interval that contains points is empty or not print("
Is the interval empty?
",interval.is_empty)

Output

This will produce the following output −

IntervalIndex...
IntervalIndex([(0, 1], (1, 2]], dtype='interval[int64, right]')

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

Is the interval empty?
[False False]
raja
Updated on 18-Oct-2021 07:44:58

Advertisements