ในการส่งคืน IntervalArray ที่เหมือนกับอันปัจจุบันแต่ปิดที่ด้านที่ระบุ ให้ใช้ set_closed() เมธอดโดยตั้งค่าพารามิเตอร์เป็น ทั้งคู่ .
ขั้นแรก นำเข้าไลบรารีที่จำเป็น -
import pandas as pd
สร้าง IntervalArray -
index = pd.arrays.IntervalArray.from_breaks(range(6))
แสดงช่วงเวลา -
print("IntervalIndex...\n",index) คืนค่า IntervalArray ที่เหมือนกันกับอันปัจจุบันแต่ปิดที่ด้านที่ระบุ เช่น "ทั้งสอง" ที่นี่ -
print("\nResult...",index.set_closed('both'))
ตัวอย่าง
ต่อไปนี้เป็นรหัส -
import pandas as pd
# Create IntervalArray
index = pd.arrays.IntervalArray.from_breaks(range(6))
# Display the interval
print("IntervalIndex...\n",index)
# Display the interval length
print("\nIntervalIndex length...\n",index.length)
# the left bound
print("\nThe left bound for the IntervalIndex...\n",index.left)
# the right bound
print("\nThe right bound for the IntervalIndex...\n",index.right)
# Return an IntervalArray identical to the current one but closed on specified
# side i.e. "both" here
print("\nResult...",index.set_closed('both')) ผลลัพธ์
สิ่งนี้จะสร้างผลลัพธ์ต่อไปนี้ -
IntervalIndex... <IntervalArray> [(0, 1], (1, 2], (2, 3], (3, 4], (4, 5]] Length: 5, dtype: interval[int64, right] IntervalIndex length... Int64Index([1, 1, 1, 1, 1], dtype='int64') The left bound for the IntervalIndex... Int64Index([0, 1, 2, 3, 4], dtype='int64') The right bound for the IntervalIndex... Int64Index([1, 2, 3, 4, 5], dtype='int64') Result... <IntervalArray> [[0, 1], [1, 2], [2, 3], [3, 4], [4, 5]] Length: 5, dtype: interval[int64, both]