要从拆分数组创建 IntervalArray,请使用pandas.arrays。.IntervalArray.from_breaks()
要检查间隔在左侧或右侧关闭,两者都关闭,或两者都不关闭,请使用该array.closed属性。
首先,导入所需的库 -
import pandas as pd
从类似数组的拆分构造一个新的 IntervalArray。默认情况下,间隔在“右侧”关闭 -
array = pd.arrays.IntervalArray.from_breaks([0, 1, 2, 3, 4, 5])
显示间隔 -
print("Our IntervalArray...\n",array)
检查 Interval Array 中的区间是否在左侧、右侧、两者或两者都不闭合 -
print("\nChecking whether the intervals is closed...\n",array.closed)
以下是代码 -
import pandas as pd # 从类似数组的拆分构造一个新的 IntervalArray # the intervals are closed on the "right" by default array = pd.arrays.IntervalArray.from_breaks([0, 1, 2, 3, 4, 5]) # 显示间隔数组 print("Our IntervalArray...\n",array) # 获取 IntervalArray 的长度 # 返回一个索引,其中的条目表示 IntervalArray 中每个 Interval 的长度 print("\nOur IntervalArray length...\n",array.length) # IntervalArray 中每个 Interval 的中点作为索引 print("\nThe midpoint of each interval in the IntervalArray...\n",array.mid) # 获得正确的端点 print("\nThe right endpoints of each Interval in the IntervalArray as an Index...\n",array.right) # 检查Interval Array中的区间是否在左侧、右侧、 # 两者兼而有之 print("\nChecking whether the intervals is closed...\n",array.closed)输出结果
这将产生以下代码 -
Our IntervalArray... <IntervalArray> [(0, 1], (1, 2], (2, 3], (3, 4], (4, 5]] Length: 5, dtype: interval[int64, right] Our IntervalArray length... Int64Index([1, 1, 1, 1, 1], dtype='int64') The midpoint of each interval in the IntervalArray... Float64Index([0.5, 1.5, 2.5, 3.5, 4.5], dtype='float64') The right endpoints of each Interval in the IntervalArray as an Index... Int64Index([1, 2, 3, 4, 5], dtype='int64') Checking whether the intervals is closed... Right