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

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


คำอธิบาย

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

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

import pandas as pd

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

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

ผลลัพธ์

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

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

เฉพาะ 2021-10-01 (เริ่ม Q4) เป็น True