Python Pandas IntervalIndex - Check if an interval with missing values is empty or not


To check if an interval with missing values is empty or not, use the IntervalIndex.is_empty property. At first, import the required libraries −

import pandas as pd
import numpy as np

Create IntervalIndex with NaN values −

interval = pd.IntervalIndex.from_arrays([np.nan, np.nan], [np.nan, np.nan])

Display the interval −

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

Check if the interval that contains missing values is empty or not −

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

Example

Following is the code −

import pandas as pd
import numpy as np

# Create IntervalIndex with NaN values
interval = pd.IntervalIndex.from_arrays([np.nan, np.nan], [np.nan, np.nan])

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

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

# check if the interval that contains missing values is empty or not
print("\nIs the interval empty?\n",interval.is_empty)

Output

This will produce the following output −

IntervalIndex...
IntervalIndex([nan, nan], dtype='interval[float64, right]')

IntervalIndex length...
Float64Index([nan, nan], dtype='float64')

Is the interval empty?
[False False]

Updated on: 18-Oct-2021

119 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements