Python Pandas - 检查区间是否在左侧和右侧打开
在 pandas 中, Interval.closed_left和Interval.closed_right属性用于检查间隔是否在左侧和右侧打开。
间隔:
- 闭区间: closed ='both' 表示闭区间。闭区间包含其端点。它的形式为 [a,b] 并且条件为 a<=x<=b。
- 开区间: closed ='既不'代表开区间。开区间不包含其端点。它的形式为 (a,b),并且条件为 a
- 左闭区间: closed ='left' 表示左闭区间。它的形式为 [a,b),并且条件为 a<=x
- 右闭区间: close='right'代表右闭区间。它的形式为 (a,b] 并且条件为 a
- 左闭区间: closed ='left' 表示左闭区间。它的形式为 [a,b),并且条件为 a<=x
检查间隔是否在左侧打开
这里导入了熊猫的包。创建了一个区间,并且“既不”被赋予封闭的参数。它代表一个开区间。在这种情况下, interval.closed_left返回'False' 。
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
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
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)
输出:
neither
False
这里,'both' 被赋予闭参数,它代表一个闭区间。在这种情况下, closed_left返回“ True ”。
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
输出:
both
True
检查间隔是否在右侧打开
默认情况下,间隔是右闭合的。如果未指定 closed 参数,则 closed 属性返回 Right 表示它是右关闭的。 Interval.closed_right属性返回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)
输出:
right
True