Python Pandas - 创建一个开放时间间隔并检查两个端点是否存在
要创建开放时间间隔,请使用并将关闭参数设置为两者都不是。要检查两个端点是否存在,请使用in属性。pandas.Interval()
首先,导入所需的库-
import pandas as pd
使用值为“两者都不是”的“关闭”参数设置打开间隔。开区间(在数学中用方括号表示)不包含其端点,#即开区间[0,5]的特征在于条件0<x<5:
interval = pd.Interval(left=0, right=20, closed='neither')
显示间隔
print("Interval...\n",interval)
检查间隔中的元素是否存在。这表明closed=两者都不包含其端点
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" 带值的参数 "neither" #Anopeninterval(inmathematicsdenotedbysquarebrackets)doesnotcontainsitsendpoints, # i.e. the open interval [0, 5] is characterized by the conditions 0 < x < 5. interval = pd.Interval(left=0, right=20, closed='neither') #displaytheinterval print("Interval...\n",interval) #displaytheinterval length print("\nInterval length...\n",interval.length) #checkfortheexistenceofanelementinanInterval #Thisshowsthatclosed=neitherdoesnotcontainitsendpoints 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