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

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


คำอธิบาย

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

ไตรมาสของปี

  • Q1: 1 ม.ค. - 31 มี.ค. (สิ้นสุด 31 มี.ค.)
  • Q2: 1 เม.ย. - 30 มิ.ย. (สิ้นสุด 30 มิ.ย.)
  • Q3: 1 ก.ค. - 30 ก.ย. (สิ้นสุด 30 ก.ย.)
  • Q4: 1 ต.ค. - 31 ธ.ค. (สิ้นสุด 31 ธ.ค.)

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

import pandas as pd

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

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

ผลลัพธ์

DateTimeIndex...
DatetimeIndex(['2021-06-15 02:30:50+09:30', '2021-06-30 02:30:50+09:30',
               '2021-07-15 02:30:50+09:30', '2021-07-30 02:30:50+09:30',
               '2021-08-14 02:30:50+09:30', '2021-08-29 02:30:50+09:30'],
              dtype='datetime64[ns, Australia/Adelaide]', freq='15D')

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

เฉพาะ 2021-06-30 (สิ้นสุด Q2) เป็น True