Python Pandas IntervalIndex - Indicates if an interval is empty (contains no points)

PythonServer Side ProgrammingProgramming

To indicate if an interval is empty (contains no points), use the interval.is_empty property in Pandas. At first, import the required libraries −

import pandas as pd

Create IntervalIndex −

interval = pd.IntervalIndex.from_arrays([0, 0], [0, 0])

Display the interval −

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

Check if the interval is empty −

print("\nIs the interval empty?\n",interval.is_empty)

Example

Following is the code −

import pandas as pd

# Create IntervalIndex
interval = pd.IntervalIndex.from_arrays([0, 0], [0, 0])

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

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

# check if the interval is empty
print("\nIs the interval empty?\n",interval.is_empty)

Output

This will produce the following output −

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

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

Is the interval empty?
[ True True]
raja
Updated on 18-Oct-2021 07:43:17

Advertisements