Python Pandas IntervalArray - 检查共享封闭端点的间隔是否重叠
要检查共享封闭端点的区间是否重叠,请使用Pandas中的方法。IntervalArray.overlaps()
首先,导入所需的库-
import pandas as pd
如果两个区间共享一个公共点(包括闭合端点),则它们会重叠。创建一个间隔数组
intervals = pd.arrays.IntervalArray.from_tuples([(10, 20), (15, 35)])
显示IntervalArray-
print("IntervalArray...\n",intervals)
检查共享封闭端点的区间是否重叠。我们在左侧设置了“关闭”参数的“左”值-
print("\nDoes interval that share closed endpoints overlap or not...\n",intervals.overlaps(pd.Interval(15,28, closed='left')))
示例
以下是代码-
import pandas as pd #Twointervalsoverlapiftheyshareacommonpoint,includingclosedendpoints #CreateanIntervalArray intervals = pd.arrays.IntervalArray.from_tuples([(10, 20), (15, 35)]) #DisplaytheIntervalArray print("IntervalArray...\n",intervals) #Displaytheintervallength print("\nInterval length...\n",intervals.length) #CheckIntervalsthatshareclosedendpointsoverlapornot # We have set closed on the left-side with the "left" 的价值 "closed" parameter print("\nDoes interval that share closed endpoints overlap or not...\n",intervals.overlaps(pd.Interval(15,28, closed='left')))输出结果
这将产生以下输出-
IntervalArray... <IntervalArray> [(10, 20], (15, 35]] Length: 2, dtype: interval[int64, right] Interval length... Int64Index([10, 20], dtype='int64') Does interval that share closed endpoints overlap or not... [ True True]