Pandas Python - 检查闭合区间是否为空


若要检查一个双闭合区间是否为空,请使用 **interval.is_empty 属性**。首先导入所需的库 -

import pandas as pd

双闭合区间。使用具有 “both” 值的 “closed” 参数设置区间

interval = pd.Interval(0, 0, closed='both')

显示区间

print("Interval...\n",interval)

当一个区间双闭合的时候,检查该区间是否为空。只包含一个点的区间不为空,即返回 False。

print("\nIs Interval empty? \n",interval.is_empty)

示例

代码如下所示

import pandas as pd

# interval closed from the both sides
# Interval set using the "closed" parameter with value "both"
interval = pd.Interval(0, 0, closed='both')

# display the interval
print("Interval...\n",interval)

# display the interval length
print("\nInterval length...\n",interval.length)

# check whether interval is empty when it is closed from both sides
# An Interval that contains a single point is not empty i.e. False is returned
print("\nIs Interval empty? \n",interval.is_empty)

输出

这将生成以下代码

Interval...
[0, 0]

Interval length...
0

Is Interval empty?
False

更新时间: 20-Oct-2021

69次浏览

开启你的 职业生涯

通过完成课程获得认证

入门
广告