Python Pandas IntervalIndex - 检查共享封闭端点的区间是否重叠
要检查共享封闭端点的区间是否重叠,请使用IntervalIndex.is_overlapping属性。首先,导入所需的库-
import pandas as pd
创建间隔索引。由于“close”参数设置为“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 #创建间隔索引 # The intervals are closed on both the sides since the "closed" 参数设置 "both" interval = pd.interval_range(0, 8, closed='both') #显示间隔 print("IntervalIndex...\n",interval) #显示间隔 length print("\nIntervalIndex length...\n",interval.length) #检查共享封闭端点的区间是否重叠 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