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

# Two intervals overlap if they share a common point, including closed endpoints
# Create an IntervalArray
intervals = pd.arrays.IntervalArray.from_tuples([(10, 20), (15, 35)])

# Display the IntervalArray
print("IntervalArray...\n",intervals)

# Display the interval length
print("\nInterval length...\n",intervals.length)

# Check Intervals that share closed endpoints overlap or not
# 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]

猜你喜欢