Python Pandas IntervalIndex - Check if Intervals that share closed endpoints overlap


To check if Intervals that share closed endpoints overlap, use the IntervalIndex.is_overlapping property. At first, import the required libraries −

import pandas as pd

Create IntervalIndex. The intervals are closed on both the sides since the "closed" parameter is set "both" −

interval = pd.interval_range(0, 8, closed='both')

Display the interval −

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

Check if the Intervals that share closed endpoints overlap −

print("\nDoes the Intervals that share closed endpoints overlap?\n",interval.is_overlapping)

Example

Following is the code −

import pandas as pd

# Create IntervalIndex
# The intervals are closed on both the sides since the "closed" parameter is set "both"
interval = pd.interval_range(0, 8, closed='both')

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

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

# Check if the Intervals that share closed endpoints overlap
print("\nDoes the Intervals that share closed endpoints overlap?\n",interval.is_overlapping)

Output

This will produce the following output −

IntervalIndex...
IntervalIndex([[0, 1], [1, 2], [2, 3], [3, 4], [4, 5], [5, 6], [6, 7], [7, 8]], dtype='interval[int64, both]')

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

Does the Intervals that share closed endpoints overlap?
True

Updated on: 18-Oct-2021

88 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements