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

Python Pandas - ตรวจสอบวันที่ใน DateTimeIndex เป็นวันสุดท้ายของปีหรือไม่


คำอธิบาย

ใช้ property DateTimeIndex.is_year_end ตรวจสอบว่าแต่ละวันที่เป็นวันที่ 31 ธันวาคม (วันสุดท้ายของปี) หรือไม่

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

import pandas as pd

datetimeindex = pd.date_range('2021-12-25 02:30:50', periods=6, tz='Australia/Adelaide', freq='2D')

print("DateTimeIndex...\n", datetimeindex)
print("\nCheck whether the date in DateTimeIndex is the last day of the year...\n", datetimeindex.is_year_end)

ผลลัพธ์

DateTimeIndex...
DatetimeIndex(['2021-12-25 02:30:50+10:30', '2021-12-27 02:30:50+10:30',
               '2021-12-29 02:30:50+10:30', '2021-12-31 02:30:50+10:30',
               '2022-01-02 02:30:50+10:30', '2022-01-04 02:30:50+10:30'],
              dtype='datetime64[ns, Australia/Adelaide]', freq='2D')

Check whether the date in DateTimeIndex is the last day of the year...
[False False False True False False]

กรณีการใช้งาน

  • กรองข้อมูลสิ้นปีบัญชี
  • คำนวณรายได้/ค่าใช้จ่ายสิ้นปี
  • จัดกลุ่มข้อมูลรายปี