คำอธิบาย
ใช้ property DateTimeIndex.date ดึง numpy array ของ datetime.date objects (เฉพาะวันที่ ไม่มีเวลา, ไม่มี timezone)
ตัวอย่างโค้ด
import pandas as pd
datetimeindex = pd.date_range('2021-10-20 02:30:50', periods=3, tz='Australia/Sydney', freq='ns')
print("DateTimeIndex...\n", datetimeindex)
print("\nThe numpy array (date part)..\n", datetimeindex.date)
ผลลัพธ์
DateTimeIndex...
DatetimeIndex(['2021-10-20 02:30:50+11:00', '2021-10-20 02:30:50.000000001+11:00',
'2021-10-20 02:30:50.000000002+11:00'], dtype='datetime64[ns, Australia/Sydney]', freq='N')
The numpy array (date part)..
[datetime.date(2021, 10, 20) datetime.date(2021, 10, 20) datetime.date(2021, 10, 20)]
กรณีการใช้งาน
- จัดกลุ่มข้อมูลตามวันที่ (group by date)
- ลบส่วนเวลาออกเพื่อ join กับข้อมูลรายวัน
- แสดงผลเฉพาะวันที่