python Pandas - Return the left endpoints of each Interval in the IntervalArray as an Index

PythonServer Side ProgrammingProgramming

To return the left endpoints of each Interval in the IntervalArray as an Index, use the array.left property.

At first, import the required libraries −

import pandas as pd

Create two Interval objects −

nterval1 = pd.Interval(10, 25)
interval2 = pd.Interval(15, 70)

Display the intervals −

print("Interval1...\n",interval1)
print("Interval2...\n",interval2)

Construct a new IntervalArray from Interval objects −

array = pd.arrays.IntervalArray([interval1,interval2])

Get the left endpoints −

print("\nThe left endpoints of each Interval in the IntervalArray as an Index...\n", array.left)

Example

Following is the code −

import pandas as pd

# Create two Interval objects
interval1 = pd.Interval(10, 25)
interval2 = pd.Interval(15, 70)

# display the intervals
print("Interval1...\n",interval1)
print("Interval2...\n",interval2)

# Construct a new IntervalArray from Interval objects
array = pd.arrays.IntervalArray([interval1,interval2])

# Display the IntervalArray
print("\nOur IntervalArray...\n",array)

# Getting the length of IntervalArray
# Returns an Index with entries denoting the length of each Interval in the IntervalArray
print("\nOur IntervalArray length...\n",array.length)

# get the left endpoints
print("\nThe left endpoints of each Interval in the IntervalArray as an Index...\n", array.left)

Output

This will produce the following code −

Interval1...
(10, 25]
Interval2...
(15, 70]

Our IntervalArray...
<IntervalArray>
[(10, 25], (15, 70]]
Length: 2, dtype: interval[int64, right]

Our IntervalArray length...
Int64Index([15, 55], dtype='int64')

The left endpoints of each Interval in the IntervalArray as an Index...
Int64Index([10, 15], dtype='int64')
raja
Updated on 12-Oct-2021 12:16:23

Advertisements