
- 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 - Create a Pandas array for interval data
To create a Pandas array for interval data, use the pandas.arrays.IntervalArray() method. At first, import the required libraries −
import pandas as pd
Create two Interval objects −
interval1 = pd.Interval(10, 30) interval2 = pd.Interval(30, 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])
Example
Following is the code −
import pandas as pd # Create two Interval objects interval1 = pd.Interval(10, 30) interval2 = pd.Interval(30, 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)
Output
This will produce the following code −
Interval1... (10, 30] Interval2... (30, 70] Our IntervalArray... <IntervalArray> [(10, 30], (30, 70]] Length: 2, dtype: interval[int64, right] Our IntervalArray length... Int64Index([20, 40], dtype='int64')
- Related Articles
- Python Pandas - Create a half-open time interval and check for existence of endpoints
- Python Pandas - Create a half-closed time interval and check for existence of endpoints
- Python Pandas - Create a closed time interval and check for existence of both the endpoints
- Python Pandas - Create a time interval and use Timestamps as the bounds
- Python Pandas - Create an open time interval and check for existence of both the endpoints
- Python Pandas - Get the right bound for the interval
- Python Pandas - Get the left bound for the interval
- Python Pandas - Check if the Pandas Index holds Interval objects
- Python Pandas - Check if an interval is empty
- Python Pandas - Get the length of the Interval
- Python Pandas - Return the midpoint of the Interval
- Python Pandas - Check whether two Interval objects overlap
- Python Pandas - Create a TimeDeltaIndex object
- Python Pandas - Create a BusinessDay offset
- Python Pandas - Create a BusinessHour offset

Advertisements