How to check if a value is contained in an IntervalArray object
The pandas toolkit was primarily designed for financial modeling, it comes with an extensive set of tools for dealing with data ranges and time series.
A pandas Interval refers to a range of values between a particular beginning and end point and having the same type.
An IntervalArray is a pandas object that represents an array of interval data that is closed on the same side.
The contains function allows checking element-wise if the intervals included within an IntervalArray contain a specific value.
This function is associated with the class pandas.arrays.IntervalArray and has the following syntax:
[Boolean] = [Interval].contains(value)
This function accepts value as a parameter to search for, and returns an array of [Boolean] values, indicating if the specified value was detected within the [Interval] intervals of the IntervalArray.
Example
Let's go through the following example showcasing how to check if a value is contained in an IntervalArray object:
import pandas as pddf = pd.DataFrame({'price_low' : [1 , 10, 25],'price_high': [10, 20, 30],'item': ['item1','item2','item3']})price_ivs = pd.arrays.IntervalArray.from_arrays(df['price_low'], df['price_high'], closed='left')print("The interval array is the following:")print(price_ivs)print("Checking whether the value 2 is contained in the IntervalArray:")print(price_ivs.contains(2))print("Checking whether the value 10 is contained in the IntervalArray:")print(price_ivs.contains(10))print("Checking whether the value 22 is contained in the IntervalArray:")print(price_ivs.contains(22))
Explanation
In the above code snippet:
Line 1: Import the
pandaslibrary.Lines 3–7: Define a DataFrame containing a list of items. Specify the low and high price for each item.
Lines 9–13: Construct an interval array based on the prices intervals set for each item and store it within a variable named
price_ivs. We set the argumentclosedtoleft, which means that the left endpoint of each interval in the interval array is inclusive (price_lowvalue) whereas the right endpoint is exclusive (price_highvalue).Lines 14–15: Display the interval array already constructed.
Lines 17–18: Check whether the value
2is contained within any interval of theprice_ivs. The boolean array returned shows that this value is included within the first interval.Lines 20–21: Check whether the value
10is contained within any interval of theprice_ivs. The boolean array returned shows that this value is included within the second interval because we set the argumentclosedtoleftas previously elaborated.Lines 23–24: Check whether the value
22is contained within theprice_ivs. The boolean array returned shows that this value is not included within the devised intervals.
Free Resources