Python Pandas IntervalIndex - 检查带闭合端点的区间是否重叠


要检查带有闭合端点的区间是否重叠,请使用 IntervalIndex.is_overlapping 属性。首先,导入所需的库:

import pandas as pd

创建 IntervalIndex。由于 "closed" 参数被设置为 "both",因此区间在两侧都闭合:

interval = pd.interval_range(0, 8, closed='both')

显示区间:

print("IntervalIndex...\n",interval)

检查带有闭合端点的区间是否重叠: -

print("\nDoes the Intervals that share closed endpoints overlap?\n",interval.is_overlapping)

示例

以下是代码: -

import pandas as pd

# Create IntervalIndex
# The intervals are closed on both the sides since the "closed" parameter is set "both"
interval = pd.interval_range(0, 8, closed='both')

# Display the interval
print("IntervalIndex...\n",interval)

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

# Check if the Intervals that share closed endpoints overlap
print("\nDoes the Intervals that share closed endpoints overlap?\n",interval.is_overlapping)

输出

这将产生以下输出:

IntervalIndex...
IntervalIndex([[0, 1], [1, 2], [2, 3], [3, 4], [4, 5], [5, 6], [6, 7], [7, 8]], dtype='interval[int64, both]')

IntervalIndex length...
Int64Index([1, 1, 1, 1, 1, 1, 1, 1], dtype='int64')

Does the Intervals that share closed endpoints overlap?
True

更新于: 18-Oct-2021

119 次浏览

开启你的职业生涯

完成课程并获得认证

开始学习
广告