Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> Python

Python Pandas - หาดัชนีระหว่างช่วงเวลาใน DateTimeIndex


คำอธิบาย

ใช้ DateTimeIndex.indexer_between_time() หาตำแหน่งดัชนีของค่าที่อยู่ในช่วงเวลาของวัน (ไม่ระบุ include_start จะเป็น False โดย mặc định)

ตัวอย่างโค้ด

import pandas as pd

datetimeindex = pd.date_range('2021-10-30 02:30:50', periods=5, tz='Australia/Adelaide', freq='20T')

print("DateTimeIndex...\n", datetimeindex)
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')

Index locations of values between particular time of day...
[0 1 2]

ความแตกต่างจาก include_start=True

  • include_start=False (default): ไม่รวมเวลาเริ่มต้นพอดี
  • include_start=True: รวมเวลาเริ่มต้นพอดี