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输出结果# Create two Interval objects
# Open interval set using the "closed" 带值的参数 "neither"
interval1 = pd.Interval(0, 0, closed='neither')
interval2 = pd.Interval(20, 50, closed='neither')
# display the intervals
print("Interval1...\n",interval1)
print("Interval2...\n",interval2)
# display the interval length
print("\nInterval1 length...\n",interval1.length)
print("\nInterval2 length...\n",interval2.length)
# Construct a new IntervalArray from Interval objects
array = pd.arrays.IntervalArray([interval1,interval2])
# Display the IntervalArray
print("\nOur IntervalArray...\n",array)
# Getting the length of IntervalArray
# Returns an Index with entries denoting the length of each Interval in the IntervalArray
print("\nOur IntervalArray length...\n",array.length)
# Check if the Intervals in the IntervalArray is empty
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]
以上是 Python Pandas - 检查 IntervalArray 中的 Intervals 是否为空 的全部内容, 来源链接: utcz.com/z/327375.html