📅  最后修改于: 2023-12-03 15:34:16.255000             🧑  作者: Mango
The Pandas library in Python provides a PeriodIndex class that represents a period of datetime. The PeriodIndex.is_leap_year method is used to check whether a year is a leap year or not.
The syntax of the PeriodIndex.is_leap_year method is as follows:
PeriodIndex.is_leap_year(year)
The method takes a single parameter:
year
- an integer value representing the year to be checked.The method returns a boolean value – True if the year is a leap year, False otherwise.
import pandas as pd
# create a PeriodIndex for a range of dates
date_range = pd.period_range('2000-01-01', '2010-12-31', freq='D')
# check if each year in the range is a leap year
for year in range(2000, 2011):
is_leap_year = date_range.is_leap_year(year)
print(f'{year} is a leap year? {is_leap_year}')
The output of the above code will be:
2000 is a leap year? True
2001 is a leap year? False
2002 is a leap year? False
2003 is a leap year? False
2004 is a leap year? True
2005 is a leap year? False
2006 is a leap year? False
2007 is a leap year? False
2008 is a leap year? True
2009 is a leap year? False
2010 is a leap year? False
In this example, a PeriodIndex is created for a range of dates from 2000 to 2010. The is_leap_year
method is used to check if each year in the range is a leap year or not. The output shows that the method correctly identifies the leap years.