ในการสร้าง 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) ตรวจสอบว่าช่วงเวลาใน Interval Array ปิดทางด้านซ้าย ด้านขวา ทั้งสองหรือไม่ -
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