Python Pandas - 检查 IntervalArray 中的区间是否在左侧、右侧、两者或两者都不闭合
要检查IntervalArray中的区间是否在左侧、右侧、两者或两者都不闭合,请使用array.closed属性。
首先,导入所需的库-
import pandas as pd
创建两个Interval对象。使用值为“both”的“closed”参数设置的闭合区间。闭区间(在数学中用方括号表示)包含其端点,即闭区间[0,5]的特征在于条件0<=x<=5-
interval1 = pd.Interval(10, 25, closed='both') interval2 = pd.Interval(15, 70, closed='both')
显示间隔-
print("Interval1...\n",interval1) print("Interval2...\n",interval2)
从Interval对象构造一个新的IntervalArray-
array = pd.arrays.IntervalArray([interval1,interval2])
检查IntervalArray中的区间是否在左侧、右侧、两者或两者都不闭合-
print("\nChecking whether the intervals is closed...\n",array.closed)
示例
以下是代码-
import pandas as pd #CreatetwoIntervalobjects # Closed intervals set using the "closed" 带值的参数 "both" #Aclosedinterval(inmathematicsdenotedbysquarebrackets)containsitsendpoints, # i.e. the closed interval [0, 5] is characterized by the conditions 0 <= x <= 5 interval1 = pd.Interval(10, 25, closed='both') interval2 = pd.Interval(15, 70, closed='both') #displaytheintervals print("Interval1...\n",interval1) print("Interval2...\n",interval2) #ConstructanewIntervalArrayfromIntervalobjects array = pd.arrays.IntervalArray([interval1,interval2]) #DisplaytheIntervalArray print("\nOur IntervalArray...\n",array) #GettingthelengthofIntervalArray #ReturnsanIndexwithentriesdenotingthelengthofeachIntervalintheIntervalArray print("\nOur IntervalArray length...\n",array.length) #checkwhethertheintervalsintheIntervalArrayisclosedontheleft-side,right-side, #bothorneither print("\nChecking whether the intervals is closed...\n",array.closed)输出结果
这将产生以下代码-
Interval1... [10, 25] Interval2... [15, 70] Our IntervalArray... <IntervalArray> [[10, 25], [15, 70]] Length: 2, dtype: interval[int64, both] Our IntervalArray length... Int64Index([15, 55], dtype='int64') Checking whether the intervals is closed... Both