Python Pandas - 检查 IntervalArray 中的 Intervals 是否为空
要检查IntervalArray中的Intervals是否为空,请使用Pandas中的array.is_empty属性。
首先,导入所需的库-
import pandas as pd
创建两个Interval对象。使用值为“neither”的“closed”参数设置的开区间-
interval1 = pd.Interval(0, 0, closed='neither') interval2 = pd.Interval(20, 50, closed='neither')
显示间隔-
print("Interval1...\n",interval1) print("Interval2...\n",interval2)
从Interval对象构造一个新的IntervalArray-
array = pd.arrays.IntervalArray([interval1,interval2])
检查IntervalArray中的Intervals是否为空-
print("\nCheck if IntervalArray is empty?\n",array.is_empty)
示例
以下是代码-
import pandas as pd #CreatetwoIntervalobjects # Open interval set using the "closed" 带值的参数 "neither" interval1 = pd.Interval(0, 0, closed='neither') interval2 = pd.Interval(20, 50, closed='neither') #displaytheintervals print("Interval1...\n",interval1) print("Interval2...\n",interval2) #displaytheintervallength print("\nInterval1 length...\n",interval1.length) print("\nInterval2 length...\n",interval2.length) #ConstructanewIntervalArrayfromIntervalobjects array = pd.arrays.IntervalArray([interval1,interval2]) #DisplaytheIntervalArray print("\nOur IntervalArray...\n",array) #GettingthelengthofIntervalArray #ReturnsanIndexwithentriesdenotingthelengthofeachIntervalintheIntervalArray print("\nOur IntervalArray length...\n",array.length) #CheckiftheIntervalsintheIntervalArrayisempty print("\nCheck if IntervalArray is empty?\n",array.is_empty)输出结果
这将产生以下代码-
Interval1... (0, 0) Interval2... (20, 50) Interval1 length... 0 Interval2 length... 30 Our IntervalArray... <IntervalArray> [(0, 0), (20, 50)] Length: 2, dtype: interval[int64, neither] Our IntervalArray length... Int64Index([0, 30], dtype='int64') Check if IntervalArray is empty? [ True False]