要从拆分数组构造 IntervalArray,请使用pandas.arrays。IntervalArray.from_breaks(). 要返回每个间隔的正确端点,请使用array.right属性。
首先,导入所需的库 -
import pandas as pd
从类似数组的拆分构造一个新的 IntervalArray -
array = pd.arrays.IntervalArray.from_breaks([0, 1, 2, 3, 4, 5])
显示间隔 -
print("Our IntervalArray...\n",array)
获得正确的端点 -
print("\nThe right endpoints of each Interval in the IntervalArray as an Index...\n",array.right)
以下是代码 -
import pandas as pd # 从类似数组的拆分构造一个新的 IntervalArray 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)输出结果
这将产生以下代码 -
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')