เพื่อให้ได้ความยาวของช่วงเวลา ให้ใช้ interval.length คุณสมบัติ. ขั้นแรก นำเข้าไลบรารีที่จำเป็น -
import pandas as pd
ตั้งค่าช่วงเวลาเปิดโดยใช้พารามิเตอร์ "ปิด" โดยมีค่าเป็น "ไม่" ช่วงเปิด (ในทางคณิตศาสตร์แสดงด้วยวงเล็บเหลี่ยม) ไม่มีจุดปลาย กล่าวคือ ช่วงเปิด [0, 5] ถูกกำหนดโดยเงื่อนไข 0
interval = pd.Interval(5, 20, closed='neither')
แสดงช่วงความยาว
print("\nInterval length...\n",interval.length)
ตัวอย่าง
ต่อไปนี้เป็นรหัส
import pandas as pd # Open interval set using the "closed" parameter with value "neither" # An open interval (in mathematics denoted by square brackets) does not contains its endpoints, # i.e. the open interval [0, 5] is characterized by the conditions 0 < x < 5. interval = pd.Interval(5, 20, closed='neither') # display the interval print("Interval...\n",interval) # the left bound print("\nThe left bound for the Interval...\n",interval.left) # the right bound print("\nThe right bound for the Interval...\n",interval.right) # display the interval length print("\nInterval length...\n",interval.length)
ผลลัพธ์
ซึ่งจะได้รหัสดังต่อไปนี้
Interval... (5, 20) The left bound for the Interval... 5 The right bound for the Interval... 20 Interval length... 15