要检查 Interval 是否在左侧关闭,请使用interval.closed_right属性。首先,导入所需的库 -
import pandas as pd
使用值为“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" 带值的参数 "right" # i.e. [0, 5) is described by 0 < x <= 5 when closed='right' interval = pd.Interval(left=0, right=20, closed='right') # 显示间隔 print("Interval...\n",interval) # 显示间隔 length print("\nInterval length...\n",interval.length) # 检查右侧的区间是否闭合 print("\nChecking whether the Interval is closed on the right...\n", interval.closed_right) # 检查间隔中元素的存在 # 这表明 closed = right 只包含最右边的端点 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