Python Pandas IntervalArray - 检查仅具有公共重叠的开放端点的区间

要检查仅具有公共重叠的开放端点的区间,请使用该overlaps()方法。

首先,导入所需的库 -

import pandas as pd

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

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

显示 IntervalArray -

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

检查仅具有公共重叠的开放端点的区间。我们在右侧设置了“关闭”参数的“右”值 -

print("\nDoes interval that that only have an open endpoint overlap or not...\n",intervals.overlaps(pd.Interval(20,25, closed='right')))

示例

以下是代码 -

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), (20, 35)])

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

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

# Check Intervals that only have an open endpoint in common overlap or not
# We have set closed on the right-side with the "right" 的价值 "closed" parameter
print("\nDoes interval that that only have an open endpoint overlap or not...\n",intervals.overlaps(pd.Interval(20,25, closed='right')))
输出结果

这将产生以下输出 -

IntervalArray...
<IntervalArray>
[(10, 20], (20, 35]]
Length: 2, dtype: interval[int64, right]

Interval length...
Int64Index([10, 15], dtype='int64')

Does interval that that only have an open endpoint overlap or not...
[False True]

猜你喜欢