ในการส่งคืนตำแหน่งดัชนีของค่าระหว่างช่วงเวลาเฉพาะของวันใน DateTimeIndex ให้ใช้ DateTimeIndex.indexer_between_time() วิธีการ
ขั้นแรก นำเข้าไลบรารีที่จำเป็น -
import pandas as pd
สร้าง DatetimeIndex ด้วยช่วงเวลา 5 และความถี่เป็น T เช่น นาที เขตเวลาคือ ออสเตรเลีย/แอดิเลด −
datetimeindex = pd.date_range('2021-10-30 02:30:50', periods=5, tz='Australia/Adelaide', freq='20T')
แสดง DateTimeIndex -
print("DateTimeIndex...\n", datetimeindex) แสดงตำแหน่งดัชนีของค่าระหว่างช่วงเวลาเฉพาะของวัน "start_time" ถูกตั้งค่าเป็น '02:30:50' และ "end_time" '03:20:50 -
print("\nIndex locations of values between particular time of day...\n",
datetimeindex.indexer_between_time('02:30:50','03:20:50')) ตัวอย่าง
ต่อไปนี้เป็นรหัส -
import pandas as pd
# DatetimeIndex with period 5 and frequency as T i.e. minutes
# The timezone is Australia/Adelaide
datetimeindex = pd.date_range('2021-10-30 02:30:50', periods=5, tz='Australia/Adelaide', freq='20T')
# display DateTimeIndex
print("DateTimeIndex...\n", datetimeindex)
# display DateTimeIndex frequency
print("\nDateTimeIndex frequency...\n", datetimeindex.freq)
# display index locations of values at particular time of day i.e. 03:10:50 here
print("\nIndex locations of values at particular time of day...\n",
datetimeindex.indexer_at_time('2021-10-30 03:10:50'))
# display index locations of values between particular time of day
# The "start_time" is set '02:30:50' and "end_time" '03:20:50'
print("\nIndex locations of values between particular time of day...\n",
datetimeindex.indexer_between_time('02:30:50','03:20:50')) ผลลัพธ์
สิ่งนี้จะสร้างรหัสต่อไปนี้ -
DateTimeIndex... DatetimeIndex(['2021-10-30 02:30:50+10:30', '2021-10-30 02:50:50+10:30', '2021-10-30 03:10:50+10:30', '2021-10-30 03:30:50+10:30', '2021-10-30 03:50:50+10:30'], dtype='datetime64[ns, Australia/Adelaide]', freq='20T') DateTimeIndex frequency... <20 * Minutes> Index locations of values at particular time of day... [2] Index locations of values between particular time of day... [0 1 2]