
- Python Basic Tutorial
- Python - Home
- Python - Overview
- Python - Environment Setup
- Python - Basic Syntax
- Python - Comments
- Python - Variables
- Python - Data Types
- Python - Operators
- Python - Decision Making
- Python - Loops
- Python - Numbers
- Python - Strings
- Python - Lists
- Python - Tuples
- Python - Dictionary
- Python - Date & Time
- Python - Functions
- Python - Modules
- Python - Files I/O
- Python - Exceptions
Python Pandas - Return the midpoint of each Interval in the IntervalArray as an Index
To return the midpoint of each Interval in the IntervalArray as an Index, use the array.mid property. At first,
At first, import the required libraries −
import pandas as pd
Create two Interval objects. Closed intervals set using the "closed" parameter with value "both" −
interval1 = pd.Interval(50, 75, closed='both') interval2 = pd.Interval(65, 90, closed='both')
Display the intervals −
print("Interval1...\n",interval1) print("Interval2...\n",interval2)
Construct a new IntervalArray from Interval objects −
array = pd.arrays.IntervalArray([interval1,interval2])
Midpoint of each Interval in the IntervalArray as an Index −
print("\nThe midpoint of each interval in the IntervalArray...\n",array.mid)
Example
Following is the code −
import pandas as pd # Create two Interval objects # Closed intervals set using the "closed" parameter with value "both" interval1 = pd.Interval(50, 75, closed='both') interval2 = pd.Interval(65, 90, closed='both') # 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) # midpoint of each Interval in the IntervalArray as an Index print("\nThe midpoint of each interval in the IntervalArray...\n",array.mid)
Output
This will produce the following code −
Interval1... [50, 75] Interval2... [65, 90] Our IntervalArray... <IntervalArray> [[50, 75], [65, 90]] Length: 2, dtype: interval[int64, both] Our IntervalArray length... Int64Index([25, 25], dtype='int64') The midpoint of each interval in the IntervalArray... Float64Index([62.5, 77.5], dtype='float64')
- Related Articles
- python Pandas - Return the left endpoints of each Interval in the IntervalArray as an Index
- Python Pandas - Return the right endpoints of each Interval in the IntervalArray as an Index
- Python Pandas - Return an Index with entries denoting the length of each Interval in the IntervalArray
- Python Pandas - Return the midpoint of the Interval
- Python Pandas - Construct an IntervalArray from an array of splits and return the left endpoints of each interval
- Python Pandas - Construct an IntervalArray from an array of splits and return the right endpoints of each interval
- Python Pandas - Construct an IntervalArray from an array-like of tuples and return the right endpoints of each Interval
- Python Pandas - Construct an IntervalArray from an array-like of tuples and return the left endpoints of each Interval
- Check elementwise if an Interval overlaps the values in the IntervalArray in Python Pandas
- Python Pandas - Check elementwise if an Interval overlaps the values in the IntervalArray created from an array of splits
- Python - Return an array representing the data in the Pandas Index
- Python Pandas - Return the Transpose of the index
- Python Pandas - Check if the Pandas Index holds Interval objects
- Python - Return the minimum value of the Pandas Index
- Python - Return the maximum value of the Pandas Index

Advertisements