คำอธิบาย
ใช้ property DateTimeIndex.hour ดึงค่าชั่วโมง (0-23) ของแต่ละ timestamp
ตัวอย่างโค้ด
import pandas as pd
datetimeindex = pd.date_range('2021-10-20 02:35:55', periods=6, tz='Australia/Sydney', freq='H')
print("DateTimeIndex...\n", datetimeindex)
print("DateTimeIndex frequency...\n", datetimeindex.freq)
print("\nGetting the hour..\n", datetimeindex.hour)
ผลลัพธ์
DateTimeIndex...
DatetimeIndex(['2021-10-20 02:35:55+11:00', '2021-10-20 03:35:55+11:00',
'2021-10-20 04:35:55+11:00', '2021-10-20 05:35:55+11:00',
'2021-10-20 06:35:55+11:00', '2021-10-20 07:35:55+11:00'],
dtype='datetime64[ns, Australia/Sydney]', freq='H')
Getting the hour..
Int64Index([2, 3, 4, 5, 6, 7], dtype='int64')