Python Pandas - 检查共享开放端点的两个 Interval 对象是否重叠
要检查共享一个开放端点的两个Interval对象是否重叠,请使用该overlaps()方法。
首先,导入所需的库-
import pandas as pd
如果两个区间共享一个公共点(包括闭合端点),则它们会重叠。只有一个共同开放端点的区间不重叠。
创建两个Interval对象。Interval1从两侧关闭。Interval2从两侧打开-
interval1 = pd.Interval(10, 30, closed='both') interval2 = pd.Interval(30, 50, closed='neither')
显示间隔-
print("Interval1...\n",interval1) print("Interval2...\n",interval2)
检查两个区间对象是否重叠-
print("\nDo both the interval objects overlap?\n",interval1.overlaps(interval2))
示例
以下是代码-
import pandas as pd #Twointervalsoverlapiftheyshareacommonpoint,includingclosedendpoints #Intervalsthatonlyhaveanopenendpointincommondonotoverlap #CreatetwoIntervalobjects #Interval1isclosedfrombothsides #Interval2isopenfrombothsides interval1 = pd.Interval(10, 30, closed='both') interval2 = pd.Interval(30, 50, closed='neither') #displaytheintervals print("Interval1...\n",interval1) print("Interval2...\n",interval2) #displaythelengthofbothInterval1andInterval2objects print("\nInterval1 object length = ",interval1.length) print("\nInterval2 object length = ",interval2.length) #checkwhetherboththeintervalobjectsoverlap print("\nDo both the interval objects overlap?\n",interval1.overlaps(interval2))输出结果
这将产生以下代码-
Interval1... [10, 30] Interval2... (30, 50) Interval1 object length = 20 Interval2 object length = 20 Do both the interval objects overlap? False