Python Pandas -检查区间是否右边是闭合的
要检查区间是否左边是闭合的,请使用 interval.closed_right 属性。首先,导入所需的库 –
import pandas as pd
使用”value”参数为“right”的“closed”参数设置的区间,即[0,5),当closed =’right’时,0<x≤5描述了该区间。
interval = pd.Interval(left = 0, right = 20, closed = 'right')
显示区间
print("Interval...\n",interval)
检查区间是否右闭
print("\nChecking whether the Interval is closed on the right...\n", interval.closed_right)
示例
以下是代码
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)
输出
这将产生以下代码
Interval...
(0, 20]
Interval length...
20
Checking whether the Interval is closed on the right...
True
极客教程