Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Python Pandas - Check if the IntervalIndex has overlapping intervals
To check if the IntervalIndex has overlapping intervals, use the IntervalIndex.is_overlapping property. This property returns True if any intervals in the index overlap with each other, and False otherwise.
Syntax
IntervalIndex.is_overlapping
Example with Overlapping Intervals
Let's create an IntervalIndex with overlapping intervals and check the property ?
import pandas as pd
# Create IntervalIndex with overlapping intervals
interval = pd.IntervalIndex.from_tuples([(10, 20), (15, 25)])
# Display the interval
print("IntervalIndex...")
print(interval)
# Display the interval length
print("\nIntervalIndex length...")
print(interval.length)
# Check if the interval is overlapping or not
print("\nIs the interval overlapping?")
print(interval.is_overlapping)
IntervalIndex... IntervalIndex([(10, 20], (15, 25]], dtype='interval[int64, right]') IntervalIndex length... Int64Index([10, 10], dtype='int64') Is the interval overlapping? True
In this example, the intervals (10, 20] and (15, 25] overlap because the range 15-20 is common to both intervals.
Example with Non-Overlapping Intervals
Let's create an IntervalIndex with non-overlapping intervals ?
import pandas as pd
# Create IntervalIndex with non-overlapping intervals
interval = pd.IntervalIndex.from_tuples([(10, 20), (25, 35), (40, 50)])
# Display the interval
print("IntervalIndex...")
print(interval)
# Check if the interval is overlapping
print("\nIs the interval overlapping?")
print(interval.is_overlapping)
IntervalIndex... IntervalIndex([(10, 20], (25, 35], (40, 50]], dtype='interval[int64, right]') Is the interval overlapping? False
Here, the intervals (10, 20], (25, 35], and (40, 50] do not overlap as there are gaps between them.
Key Points
- The
is_overlappingproperty is read-only and returns a boolean value - Two intervals overlap if they share any common values
- Adjacent intervals with touching endpoints are not considered overlapping
- Empty IntervalIndex returns
False
Conclusion
The is_overlapping property provides a quick way to determine if any intervals in an IntervalIndex overlap. This is useful for data validation and ensuring interval data integrity in time series and range-based analysis.
