Python Pandas - 检查区间在右侧是否闭合


若要检查某区间是否在左侧闭合,请使用 interval.closed_right 属性。首先,导入所需的库 -

import pandas as pd

使用值 "right" 的 "closed" 参数设置区间,即 [0, 5) 采用闭合 "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

更新于: 20-Oct-2021

146 次浏览

启动你的 职业生涯

通过完成课程获得认证

开始
广告