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. This property returns a Boolean array indicating whether each interval is empty.

Syntax

IntervalIndex.is_empty

Understanding IntervalIndex with NaN Values

When creating intervals with NaN values, the is_empty property behaves differently than you might expect ?

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...")
print(interval)
IntervalIndex...
IntervalIndex([nan, nan], dtype='interval[float64, right]')

Checking if Intervals are Empty

Use the is_empty property to check each interval ?

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])

# Check if the intervals are empty
print("Is the interval empty?")
print(interval.is_empty)

# Display the interval length for comparison
print("\nIntervalIndex length...")
print(interval.length)
Is the interval empty?
[False False]

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

Comparison with Regular Intervals

Let's compare NaN intervals with regular and truly empty intervals ?

import pandas as pd
import numpy as np

# Create different types of intervals
nan_interval = pd.IntervalIndex.from_arrays([np.nan, np.nan], [np.nan, np.nan])
normal_interval = pd.IntervalIndex.from_arrays([1, 3], [5, 7])
empty_interval = pd.IntervalIndex.from_arrays([1, 3], [1, 3])

print("NaN intervals is_empty:", nan_interval.is_empty)
print("Normal intervals is_empty:", normal_interval.is_empty)
print("Empty intervals is_empty:", empty_interval.is_empty)
NaN intervals is_empty: [False False]
Normal intervals is_empty: [False False]
Empty intervals is_empty: [ True  True]

Key Points

Interval Type is_empty Result Explanation
NaN intervals False NaN intervals are not considered empty
Normal intervals False Intervals with different left/right bounds
Empty intervals True Intervals where left equals right bound

Conclusion

The is_empty property returns False for intervals with NaN values, as they are not considered empty. Only intervals where the left bound equals the right bound return True.

Updated on: 2026-03-26T16:59:13+05:30

227 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements