# Python Pandas - Check whether the interval is closed on the left-side, right-side, both or neither

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 whether the interval is closed on the left-side, right-side, both or neither, use the interval.closed property.

At first, import the required libraries −

import pandas as pd

Closed interval set using the "closed" parameter with value "both". A closed interval (in mathematics denoted by square brackets) contains its endpoints, # i.e. the closed interval [0, 5] is characterized by the conditions 0 <= x <= 5

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

Display the interval

print("Interval...",interval)

Check whether the interval is closed on the left-side, right-side, both or neither

print("Checking for the type of Interval...",interval.closed)

## Example

Following is the code

import pandas as pd

# Closed interval set using the "closed" parameter with value "both"
# A closed interval (in mathematics denoted by square brackets) contains its endpoints,
# i.e. the closed interval [0, 5] is characterized by the conditions 0 <= x <= 5.
interval = pd.Interval(left=0, right=20, closed='both')

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

# check whether the interval is closed on the left-side, right-side, both or neither
print("Checking for the type of Interval...",interval.closed)

# check for the existence of an element in an Interval
# This shows that closed = both contains its endpoints
print("The left-most element exists in the Interval? = ",0 in interval)
print("The right-most element exists in the Interval? = ",20 in interval)

## Output

This will produce the following code

Interval...
[0, 20]

Checking for the type of Interval...
both


Updated on 20-Oct-2021 07:54:37