Python Pandas IntervalArray - 检查共享闭合端点的区间是否重叠


要检查共享闭合端点的区间是否重叠,请在 Pandas 中使用 **IntervalArray.overlaps()** 方法。

首先,导入所需的库 -

import pandas as pd

如果两个区间共享一个公共点,包括闭合端点,则它们重叠。创建 IntervalArray

intervals = pd.arrays.IntervalArray.from_tuples([(10, 20), (15, 35)])

显示 IntervalArray -

print("IntervalArray...\n",intervals)

检查共享闭合端点的区间是否重叠。我们使用 “closed” 参数的 “left” 值在左侧设置了闭合 -

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" value of the "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]

更新于: 2021-10-19

77 次浏览

开启你的 职业生涯

通过完成课程获得认证

立即开始
广告