In pandas Interval.closed_left and Interval.closed_right attributes are used to check if the interval is open on left and right side.
Intervals:
- Closed interval : closed =’both’ represents closed interval. The closed interval contains its endpoints. it is of the form [a,b] and it has the condition a<=x<=b.
- Open interval: closed =’ neither’ represents open interval. Open interval doesn’t contain its endpoints. it is of the form (a,b) and it has the condition a<x<b.
- Left closed interval: closed =’left’ represents left closed interval. It is in the form [a,b) and it has the condition a<=x<b.
- Right closed interval: closed =’ right’ represents right closed interval. It is in the form (a,b] and it has the condition a<x<=b.
Check if the interval is open on the left side
Here, the panda’s package is imported. An interval is created and ‘neither’ is given to the closed parameter. It represents an open interval. interval.closed_left returns ‘False’ in this case.
Python3
import pandas as pd # creating intervals # setting the closed parameter to # 'neither'.it is also called open interval # an interval open on both sides # is of the form a<x<b interval1 = pd.Interval( 2 , 10 , closed = 'neither' ) print (interval1.closed) print (interval1.closed_left) |
Output:
neither False
Here, ‘both’ is given to the closed parameter, it represents a closed interval. closed_left returns ‘True‘ in this case.
Python3
import pandas as pd # creating intervals # setting the closed parameter to 'both'. # it is also called closed interval # an interval closed on both sides is # of the form a<x<b interval1 = pd.Interval( 2 , 10 , closed = 'both' ) print (interval1.closed) print (interval1.closed_left) |
Output:
both True
Check if the interval is open on the right side
By default, intervals are right closed. When the closed parameter isn’t specified, the closed attribute returns Right which says it is right closed. Interval.closed_right attribute returns True.
Python3
# import packages import pandas as pd # creating intervals interval1 = pd.Interval( 2 , 10 ) # by default intervals are 'right' closed print (interval1.closed) print (interval1.closed_right) |
Output:
right True