Python is a great language for doing data analysis, primarily because of the fantastic ecosystem of data-centric python packages. Pandas is one of those packages and makes importing and analyzing data much easier.
Pandas DatetimeIndex.is_leap_year
attribute return a boolean indicator if the date belongs to a leap year. A leap year is a year, which has 366 days (instead of 365) including 29th of February as an intercalary day. Leap years are years which are multiples of four with the exception of years divisible by 100 but not by 400.
Syntax: DatetimeIndex.is_leap_year
Returns: numpy array containing logical values.
Example #1: Use DatetimeIndex.is_leap_year
attribute to check if the dates present in the DatetimeIndex object belongs to a leap year.
# importing pandas as pd import pandas as pd # Create the DatetimeIndex didx = pd.DatetimeIndex([ '2014-01-01' , '2008-12-31' , '2017-03-31' , '2000-12-31' ]) # Print the DatetimeIndex print (didx) |
Output :
Now we want to find if the dates contained in the given DatetimeIndex object belongs to a leap year or not.
# find if the dates belong to leap year didx.is_leap_year |
Output :
As we can see in the output, the function has returned a numpy array containing logical values for each entry of the DatetimeIndex object. True
values indicate the corresponding date belongs to a leap year and False
value indicate the corresponding date does not belongs to a leap year.
Example #2: Use DatetimeIndex.is_leap_year
attribute to check if the dates present in the DatetimeIndex object belongs to a leap year.
# importing pandas as pd import pandas as pd # Create the DatetimeIndex didx = pd.date_range( "2008-12-30" , periods = 5 , freq = 'Q' ) # Print the DatetimeIndex print (didx) |
Output :
Now we want to find if the dates contained in the given DatetimeIndex object belongs to a leap year or not.
# find if the dates belong to leap year didx.is_leap_year |
Output :
As we can see in the output, the function has returned a numpy array containing logical values for each entry of the DatetimeIndex object. True
values indicate the corresponding date belongs to a leap year and False
value indicate the corresponding date does not belongs to a leap year.