
- 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 - Check if the Intervals in the IntervalArray is empty
To check if the Intervals in the IntervalArray is empty, use the array.is_empty property in Pandas.
At first, import the required libraries −
import pandas as pd
Create two Interval objects. Open interval set using the "closed" parameter with value "neither" −
interval1 = pd.Interval(0, 0, closed='neither') interval2 = pd.Interval(20, 50, closed='neither')
Display the intervals −
print("Interval1...\n",interval1) print("Interval2...\n",interval2)
Construct a new IntervalArray from Interval objects −
array = pd.arrays.IntervalArray([interval1,interval2])
Check if the Intervals in the IntervalArray is empty −
print("\nCheck if IntervalArray is empty?\n",array.is_empty)
Example
Following is the code −
import pandas as pd # Create two Interval objects # Open interval set using the "closed" parameter with value "neither" interval1 = pd.Interval(0, 0, closed='neither') interval2 = pd.Interval(20, 50, closed='neither') # display the intervals print("Interval1...\n",interval1) print("Interval2...\n",interval2) # display the interval length print("\nInterval1 length...\n",interval1.length) print("\nInterval2 length...\n",interval2.length) # 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) # Check if the Intervals in the IntervalArray is empty print("\nCheck if IntervalArray is empty?\n",array.is_empty)
Output
This will produce the following code −
Interval1... (0, 0) Interval2... (20, 50) Interval1 length... 0 Interval2 length... 30 Our IntervalArray... <IntervalArray> [(0, 0), (20, 50)] Length: 2, dtype: interval[int64, neither] Our IntervalArray length... Int64Index([0, 30], dtype='int64') Check if IntervalArray is empty? [ True False]
- Related Articles
- Python Pandas IntervalArray - Check Intervals that share closed endpoints overlap or not
- Python Pandas - Check if the IntervalIndex has overlapping intervals
- Python Pandas - Check elementwise if the Intervals contain the value
- Check elementwise if an Interval overlaps the values in the IntervalArray in Python Pandas
- Python Pandas - Check if an interval is empty
- Python Pandas - Check whether the intervals in IntervalArray are closed on the left-side, right-side, both or neither
- Python Pandas - Check if the index is empty with 0 elements
- Python Pandas - Check elementwise if the Intervals in the IntervalIndex contain the value
- Python Pandas IntervalArray - Check Intervals that only have an open endpoint in common overlap or not
- Check elementwise if the Intervals in the IntervalIndex contain the value in Python Pandas
- Python Pandas - Check if an interval is empty if closed from the left side
- Python Pandas - Check if an interval is empty if closed from the both sides
- Python Pandas - Check if an interval set as open is empty
- Python Pandas IntervalIndex - Check if Intervals that share closed endpoints overlap
- Python Pandas - Check elementwise if an Interval overlaps the values in the IntervalArray created from an array of splits

Advertisements