Python Pandas - Check if an element belongs to an Interval


To check if an element belongs to an Interval, use the in property. At first, import the required libraries −

import pandas as pd

Create a time interval

interval = pd.Interval(left=0, right=10)

Display the interval

print("Interval...\n",interval)

Check for the existence of an element in an Interval

print("\nThe specific element exists in the Interval? = \n",6 in interval)

Example

Following is the code

import pandas as pd

# Create a time interval
interval = pd.Interval(left=0, right=10)

# display the interval
print("Interval...\n",interval)

# display the interval length
print("\nInterval length...\n",interval.length)

# check for the existence of an element in an Interval
print("\nThe specific element exists in the Interval? = \n",6 in interval)

Output

This will produce the following code

Interval...
(0, 10]

Interval length...
10

The specific element exists in the Interval? =
True

Updated on: 20-Oct-2021

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements