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

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


คำอธิบาย

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

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

import pandas as pd

datetimeindex = pd.date_range('2021-9-15 06:40:35', periods=6, tz='Australia/Adelaide', freq='15D')

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

ผลลัพธ์

DateTimeIndex...
DatetimeIndex(['2021-09-15 06:40:35+09:30', '2021-09-30 06:40:35+09:30',
               '2021-10-15 06:40:35+10:30', '2021-10-30 06:40:35+10:30',
               '2021-11-14 06:40:35+10:30', '2021-11-29 06:40:35+10:30'],
              dtype='datetime64[ns, Australia/Adelaide]', freq='15D')

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

เฉพาะ 2021-09-30 (สิ้นสุด กันยายน) เป็น True