Python Pandas - 检查 IntervalArray 中的区间是否在左侧、右侧、两侧或都不闭合
要检查 IntervalArray 中的区间是否在左侧、右侧、两侧或都不闭合,请使用 **array.closed** 属性。
首先,导入所需的库:
import pandas as pd
创建两个 Interval 对象。使用值为“both”的“closed”参数设置闭合区间。闭区间(在数学中用方括号表示)包含其端点,即闭区间 [0, 5] 的特征在于条件 0 <= x <= 5:
interval1 = pd.Interval(10, 25, closed='both') interval2 = pd.Interval(15, 70, closed='both')
显示区间:
print("Interval1...\n",interval1) print("Interval2...\n",interval2)
从 Interval 对象构造一个新的 IntervalArray:
array = pd.arrays.IntervalArray([interval1,interval2])
检查 IntervalArray 中的区间是否在左侧、右侧、两侧或都不闭合:
print("\nChecking whether the intervals is closed...\n",array.closed)
示例
以下是代码:
import pandas as pd # Create two Interval objects # Closed intervals set using the "closed" parameter with value "both" # A closed interval (in mathematics denoted by square brackets) contains its endpoints, # i.e. the closed interval [0, 5] is characterized by the conditions 0 <= x <= 5 interval1 = pd.Interval(10, 25, closed='both') interval2 = pd.Interval(15, 70, closed='both') # display the intervals print("Interval1...\n",interval1) print("Interval2...\n",interval2) # 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 whether the intervals in the Interval Array is closed on the left-side, right-side, # both or neither print("\nChecking whether the intervals is closed...\n",array.closed)
输出
这将产生以下代码:
Interval1... [10, 25] Interval2... [15, 70] Our IntervalArray... <IntervalArray> [[10, 25], [15, 70]] Length: 2, dtype: interval[int64, both] Our IntervalArray length... Int64Index([15, 55], dtype='int64') Checking whether the intervals is closed... Both
广告