เพื่อตรวจสอบว่า Interval ปิดทางซ้าย ขวา ทั้งสองข้าง หรือไม่ปิดเลย ให้ใช้ property interval.closed
ขั้นตอนการทำงาน
1. นำเข้าไลบรารีที่จำเป็น
import pandas as pd
2. สร้าง Interval ที่ปิดทั้งสองข้าง
ใช้พารามิเตอร์ closed='both' สำหรับช่วงปิดทั้งสองข้าง ซึ่งรวมจุดปลายทั้งสองข้าง เช่น [0, 5] หมายถึง 0 ≤ x ≤ 5
interval = pd.Interval(left=0, right=20, closed='both')
3. แสดง Interval
print("Interval...\n", interval)
4. ตรวจสอบประเภทของ Interval
print("\nChecking for the type of Interval...\n", interval.closed)
ตัวอย่างโค้ดสมบูรณ์
import pandas as pd
# สร้าง interval ปิดทั้งสองข้าง
interval = pd.Interval(left=0, right=20, closed='both')
# แสดง interval
print("Interval...\n", interval)
# ตรวจสอบประเภท interval
print("\nChecking for the type of Interval...\n", interval.closed)
# ตรวจสอบการมีอยู่ของจุดปลาย
print("\nThe left-most element exists in the Interval? = \n", 0 in interval)
print("\nThe right-most element exists in the Interval? = \n", 20 in interval)
ผลลัพธ์ที่คาดหวัง
Interval... [0, 20] Checking for the type of Interval... both The left-most element exists in the Interval? = True The right-most element exists in the Interval? = True
คำอธิบาย: Property closed จะคืนค่าเป็นสตริง: 'left', 'right', 'both', หรือ 'neither' ตามประเภทของ interval