# Python Pandas - Check if an Interval is closed on the right side

PythonServer Side ProgrammingProgramming

#### Beyond Basic Programming - Intermediate Python

Most Popular

36 Lectures 3 hours

#### Practical Machine Learning using Python

Best Seller

91 Lectures 23.5 hours

#### Practical Data Science using Python

22 Lectures 6 hours

To check if an Interval is closed on the left side, use the interval.closed_right property. At first, import the required libraries −

import pandas as pd

Interval set using the "closed" parameter with value "right" i.e. [0, 5) is described by 0 < x <= 5 when closed='right'

interval = pd.Interval(left=0, right=20, closed='right')


Display the interval

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

Check whether the interval is closed on the right-side

print("\nChecking whether the Interval is closed on the right...\n", interval.closed_right)


## Example

Following is the code

import pandas as pd

# Interval set using the "closed" parameter with value "right"
# i.e. [0, 5) is described by 0 < x <= 5 when closed='right'
interval = pd.Interval(left=0, right=20, closed='right')

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

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

# check whether the interval is closed on the right-side
print("\nChecking whether the Interval is closed on the right...\n", interval.closed_right)

# check for the existence of an element in an Interval
# This shows that closed = right contain only the right-most endpoint
print("\nThe left-most element exists in the Interval? = \n",0 in interval)
print("\nThe right-most element exists in the Interval? = \n",20 in interval)

## Output

This will produce the following code

Interval...
(0, 20]

Interval length...
20

Checking whether the Interval is closed on the right...
True