Python Pandas - 检查两个 Interval 对象是否重叠
要检查两个 Interval 对象是否重叠,请使用 overlaps() 方法。首先,导入所需的库 -
import pandas as pd
如果两个区间有一个公共点(包括闭合端点),则它们重叠。仅有一个开放端点的区间不重叠。创建两个 Interval 对象
interval1 = pd.Interval(10, 30) interval2 = pd.Interval(25, 35)
显示区间
print("Interval1...\n",interval1) print("Interval2...\n",interval2)
检查两个区间对象是否重叠
print("\nDo both the interval objects overlap?\n",interval1.overlaps(interval2))
示例
以下是代码
import pandas as pd # Two intervals overlap if they share a common point, including closed endpoints # Intervals that only have an open endpoint in common do not overlap # create two Interval objects interval1 = pd.Interval(10, 30) interval2 = pd.Interval(25, 35) # display the intervals print("Interval1...\n",interval1) print("Interval2...\n",interval2) # display the length of both Interval1 and Interval2 objects print("\nInterval1 object length = ",interval1.length) print("\nInterval2 object length = ",interval2.length) # check whether both the interval objects overlap print("\nDo both the interval objects overlap?\n",interval1.overlaps(interval2))
输出
这将生成以下代码
Interval1... (10, 30] Interval2... (25, 35] Interval1 object length = 20 Interval2 object length = 10 Do both the interval objects overlap? True
广告