Python Pandas - 从分割数组创建 IntervalArray 并检查区间是左闭右开、左开右闭、左右都闭还是都不闭
要从分割数组创建 IntervalArray,请使用 **pandas.arrays.IntervalArray.from_breaks()**。
要检查区间是左闭右开、左开右闭、左右都闭还是都不闭,请使用 array.closed 属性。
首先,导入所需的库:
import pandas as pd
从类似数组的分割创建新的 IntervalArray。默认情况下,区间在“右侧”闭合:
array = pd.arrays.IntervalArray.from_breaks([0, 1, 2, 3, 4, 5])
显示区间:
print("Our IntervalArray...\n",array)检查 IntervalArray 中的区间是左闭、右闭、左右都闭还是都不闭:
print("\nChecking whether the intervals is closed...\n",array.closed)
示例
以下是代码:
import pandas as pd
# Construct a new IntervalArray from an array-like of splits
# the intervals are closed on the "right" by default
array = pd.arrays.IntervalArray.from_breaks([0, 1, 2, 3, 4, 5])
# Display the IntervalArray
print("Our 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)
# midpoint of each Interval in the IntervalArray as an Index
print("\nThe midpoint of each interval in the IntervalArray...\n",array.mid)
# get the right endpoints
print("\nThe right endpoints of each Interval in the IntervalArray as an Index...\n",array.right)
# 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)输出
这将产生以下代码:
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') Checking whether the intervals is closed... Right
广告
数据结构
网络
关系型数据库管理系统 (RDBMS)
操作系统
Java
iOS
HTML
CSS
Android
Python
C语言编程
C++
C#
MongoDB
MySQL
Javascript
PHP