ในการตรวจสอบว่าวันที่ใน DateTimeIndex เป็นของปีอธิกสุรทินหรือไม่ ให้ใช้ DateTimeIndex.is_leap_year ทรัพย์สิน
ขั้นแรก นำเข้าไลบรารีที่จำเป็น -
import pandas as pd
สร้าง DatetimeIndex ด้วยระยะเวลา 6 และความถี่เป็น Y เช่นปี -
datetimeindex = pd.date_range('2021-12-30 02:30:50', periods=6, tz='Australia/Adelaide', freq='3Y')
แสดง DateTimeIndex -
print("DateTimeIndex...\n", datetimeindex) ตรวจสอบว่าวันที่ใน DateTimeIndex เป็นปีอธิกสุรทินหรือไม่ -
print("\nCheck whether the date in DateTimeIndex belongs to a leap year or not...\n",
datetimeindex.is_leap_year) ตัวอย่าง
ต่อไปนี้เป็นรหัส -
import pandas as pd
# DatetimeIndex with period 6 and frequency as Y i.e. years
# The timezone is Australia/Adelaide
datetimeindex = pd.date_range('2021-12-30 02:30:50', periods=6, tz='Australia/Adelaide', freq='3Y')
# display DateTimeIndex
print("DateTimeIndex...\n", datetimeindex)
# display DateTimeIndex frequency
print("\nDateTimeIndex frequency...\n", datetimeindex.freq)
# Check whether the date in DateTimeIndex is a leap year or not
print("\nCheck whether the date in DateTimeIndex belongs to a leap year or not...\n",
datetimeindex.is_leap_year) ผลลัพธ์
สิ่งนี้จะสร้างรหัสต่อไปนี้ -
DateTimeIndex... DatetimeIndex(['2021-12-31 02:30:50+10:30', '2024-12-31 02:30:50+10:30', '2027-12-31 02:30:50+10:30', '2030-12-31 02:30:50+10:30', '2033-12-31 02:30:50+10:30', '2036-12-31 02:30:50+10:30'], dtype='datetime64[ns, Australia/Adelaide]', freq='3A-DEC') DateTimeIndex frequency... <3 * YearEnds: month=12> Check whether the date in DateTimeIndex belongs to a leap year or not... [False True False False False True]