Python Pandas - Check if the IntervalIndex has overlapping intervals

PythonServer Side ProgrammingProgramming

To check if the IntervalIndex has overlapping intervals, use the Intervalndex.is_overlapping property. At first, import the required libraries −

import pandas as pd

Create IntervalIndex −

interval = pd.IntervalIndex.from_tuples([(10, 20), (15, 25)])

Display the interval −

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

Check if the interval is overlapping or not −

print("\nIs the interval overlapping?\n",interval.is_overlapping)

Example

Following is the code −

import pandas as pd

# Create IntervalIndex
interval = pd.IntervalIndex.from_tuples([(10, 20), (15, 25)])

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

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

# Check if the interval is overlapping or not
print("\nIs the interval overlapping?\n",interval.is_overlapping)

Output

This will produce the following output −

IntervalIndex...
IntervalIndex([(10, 20], (15, 25]], dtype='interval[int64, right]')

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

Is the interval overlapping?
True
raja
Published on 18-Oct-2021 07:50:01
Advertisements