Python Pandas - 返回一个与当前相同但在左侧关闭的 IntervalArray
要返回与当前相同但在左侧关闭的IntervalArray,请使用set_closed()值为left的方法。
首先,导入所需的库-
import pandas as pd
创建IntervalArray-
index = pd.arrays.IntervalArray.from_breaks(range(5))
显示间隔-
print("IntervalIndex...\n",index)
返回一个与当前相同但在指定一侧关闭的IntervalArray,即此处的“左”-
print("\nResult...",index.set_closed('left'))
示例
以下是代码-
import pandas as pd #创建间隔数组 index = pd.arrays.IntervalArray.from_breaks(range(5)) #显示间隔 print("IntervalIndex...\n",index) #显示间隔 length print("\nIntervalIndex length...\n",index.length) #左边界 print("\nThe left bound for the IntervalIndex...\n",index.left) #正确的界限 print("\nThe right bound for the IntervalIndex...\n",index.right) #返回一个与当前相同但在指定时间关闭的IntervalArray # side i.e. "left" here print("\nResult...",index.set_closed('left'))输出结果
这将产生以下输出-
IntervalIndex... <IntervalArray> [(0, 1], (1, 2], (2, 3], (3, 4]] Length: 4, dtype: interval[int64, right] IntervalIndex length... Int64Index([1, 1, 1, 1], dtype='int64') The left bound for the IntervalIndex... Int64Index([0, 1, 2, 3], dtype='int64') The right bound for the IntervalIndex... Int64Index([1, 2, 3, 4], dtype='int64') Result... <IntervalArray> [[0, 1), [1, 2), [2, 3), [3, 4)] Length: 4, dtype: interval[int64, left]