Python Pandas IntervalIndex - Return an ndarray of tuples of the form (left, right)


To return an ndarray of tuples of the form left, right, use the to_tuples() method in Pandas. At first, import the required libraries −

import pandas as pd

Create IntervalArray −

index = pd.arrays.IntervalArray.from_breaks(range(5))

Display the interval −

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

Return the ndarray of Tuples −

print("\nThe ndarray of Tuples...\n",index.to_tuples())

Example

Following is the code −

import pandas as pd

# Create IntervalArray
index = pd.arrays.IntervalArray.from_breaks(range(5))

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

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

# Return the ndarray of Tuples
print("\nThe ndarray of Tuples...\n",index.to_tuples())

Output

This will produce the following output −

IntervalIndex...
<IntervalArray>
[(0, 1], (1, 2], (2, 3], (3, 4]]
Length: 4, dtype: interval[int64, right]

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

The ndarray of Tuples...
[(0, 1) (1, 2) (2, 3) (3, 4)]

Updated on: 19-Oct-2021

123 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements