Python Pandas – 创建一个开放时间间隔并检查两个端点是否存在

Python Pandas – 创建一个开放时间间隔并检查两个端点是否存在

要创建一个开放的时间间隔,使用 pandas.Interval() 并将 closed 参数设置为 neither。要检查两个端点是否存在,使用 in 属性。

首先,导入所需的库 –

import pandas as pd

使用 closed 参数的值为 neither 来设置开放区间。 开放区间(在数学中用方括号表示)不包含其端点,即开放区间 [0, 5] 的特点是 0 < x < 5:

interval = pd.Interval(left=0, right=20, closed='neither')

显示时间间隔:

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

检查时间间隔中元素的存在。这表明closed = neither 不包含其端点:

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)

示例

以下是代码:

import pandas as pd

# Open interval set using the "closed" parameter with value "neither"
# An open interval (in mathematics denoted by square brackets) does not contains its endpoints,
# i.e. the open interval [0, 5] is characterized by the conditions 0 < x < 5.
interval = pd.Interval(left=0, right=20, closed='neither')

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

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

# check for the existence of an element in an Interval
# This shows that closed = neither does not contain its endpoints
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

Python教程

Java教程

Web教程

数据库教程

图形图像教程

大数据教程

开发工具教程

计算机教程