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

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


คำอธิบาย

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

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

import pandas as pd

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

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

ผลลัพธ์

DateTimeIndex...
DatetimeIndex(['2021-09-21 02:30:50+09:30', '2021-09-26 02:30:50+09:30',
               '2021-10-01 02:30:50+09:30', '2021-10-06 02:30:50+10:30',
               '2021-10-11 02:30:50+10:30', '2021-10-16 02:30:50+10:30'],
              dtype='datetime64[ns, Australia/Adelaide]', freq='5D')

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

เฉพาะ 2021-10-01 เป็น True