Python Pandas - Get the midpoint from the IntervalIndex

To get the midpoint from the IntervalIndex, use the interval.mid property in Pandas. The midpoint is calculated as the average of the left and right bounds of each interval.

Creating an IntervalIndex

First, let's create an IntervalIndex using from_arrays() method ?

import pandas as pd

# Create IntervalIndex from left and right bounds
interval = pd.IntervalIndex.from_arrays([10, 15, 20], [20, 25, 30])
print("IntervalIndex...")
print(interval)
IntervalIndex...
IntervalIndex([(10, 20], (15, 25], (20, 30]], dtype='interval[int64, right]')

Getting the Midpoint

Use the .mid property to calculate the midpoint of each interval ?

import pandas as pd

interval = pd.IntervalIndex.from_arrays([10, 15, 20], [20, 25, 30])
print("Midpoints:")
print(interval.mid)
Midpoints:
Float64Index([15.0, 20.0, 25.0], dtype='float64')

Complete Example

Here's a comprehensive example showing IntervalIndex properties including midpoints ?

import pandas as pd

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

# Display the interval
print("IntervalIndex...")
print(interval)

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

# Check whether the IntervalIndex is closed on the left-side, right-side, both or neither
print("\nChecking for the type of IntervalIndex...")
print(interval.closed)

# the left bound
print("\nThe left bound for the IntervalIndex...")
print(interval.left)

# the right bound
print("\nThe right bound for the IntervalIndex...")
print(interval.right)

# return the midpoint of the Interval
print("\nThe midpoint for the Interval...")
print(interval.mid)
IntervalIndex...
IntervalIndex([(10, 20], (15, 25], (20, 30]], dtype='interval[int64, right]')

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

Checking for the type of IntervalIndex...
right

The left bound for the IntervalIndex...
Int64Index([10, 15, 20], dtype='int64')

The right bound for the IntervalIndex...
Int64Index([20, 25, 30], dtype='int64')

The midpoint for the Interval...
Float64Index([15.0, 20.0, 25.0], dtype='float64')

Key Properties

Property Description Example Result
.mid Midpoint of intervals [15.0, 20.0, 25.0]
.left Left bounds [10, 15, 20]
.right Right bounds [20, 25, 30]
.length Length of intervals [10, 10, 10]

Conclusion

The .mid property calculates the midpoint as (left + right) / 2 for each interval. This returns a Float64Index containing the calculated midpoints, making it useful for statistical analysis and data binning operations.

Updated on: 2026-03-26T16:58:08+05:30

505 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements