
- 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 elementwise if the Intervals contain the value
To check elementwise if the Intervals contain the value, use the array.contains() method.
At first, import the required libraries −
import pandas as pd
Construct a new IntervalArray from an array-like of splits −
array = pd.arrays.IntervalArray.from_breaks([0, 1, 2, 3, 4, 5])
Display the intervals −
print("Our IntervalArray...\n",array)
Check whether the Interval contain a specific value −
print("\nDoes the Intervals contain the value? \n",array.contains(3.5))
Example
Following is the code −
import pandas as pd # Construct a new IntervalArray from an array-like of splits array = pd.arrays.IntervalArray.from_breaks([0, 1, 2, 3, 4, 5]) # Display the IntervalArray print("Our 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) # get the right endpoints print("\nThe right endpoints of each Interval in the IntervalArray as an Index...\n",array.right) print("\nDoes the Intervals contain the value? \n",array.contains(3.5))
Output
This will produce the following code −
Our IntervalArray... <IntervalArray> [(0, 1], (1, 2], (2, 3], (3, 4], (4, 5]] Length: 5, dtype: interval[int64, right] Our IntervalArray length... Int64Index([1, 1, 1, 1, 1], dtype='int64') The midpoint of each interval in the IntervalArray... Float64Index([0.5, 1.5, 2.5, 3.5, 4.5], dtype='float64') The right endpoints of each Interval in the IntervalArray as an Index... Int64Index([1, 2, 3, 4, 5], dtype='int64') Does the Intervals contain the value? [False False False True False]
- Related Articles
- Python Pandas - Check elementwise if the Intervals in the IntervalIndex contain the value
- Check elementwise if the Intervals in the IntervalIndex contain the value in Python Pandas
- Python Pandas - Check if the IntervalIndex has overlapping intervals
- Check elementwise if an Interval overlaps the values in the IntervalArray in Python Pandas
- Python Pandas - Check if the Intervals in the IntervalArray 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
- Python Pandas - Determine if two CategoricalIndex objects contain the same elements
- Python Pandas - Check if the Pandas Index holds Interval objects
- Python - Check if list contain particular digits
- Python Pandas - Check if the index has NaNs
- Python - Check if a List contain particular digits
- Python Pandas IntervalArray - Check Intervals that share closed endpoints overlap or not
- Python Pandas IntervalIndex - Check if Intervals that only have an open endpoint in common overlap or not
- Python - Check if the Pandas Index holds categorical data

Advertisements