Pandas – Check if Year is Leap Year or Not

In this tutorial, we will look at how to check if the year in a date is leap year or not in pandas with the help of some examples.

Check if date is in a leap year pandas

It can be handy to know how to quickly check whether a year is leap year or not without writing a custom function. There’s a custom pandas datetime object attribute that you can use. Datetime objects in pandas come with a number of useful attributes. For example, you can use such attributes to extract the year or month from a pandas date.

To check whether a date is in a leap year or not, you can use the pandas .is_leap_year attribute. It returns True if the year is a leap year and False otherwise. Here’s an example –

import pandas as pd

# create a sample date
sample_date = pd.Timestamp(2012, 4, 1)
# check if sample_date was in a leap year
sample_date.is_leap_year

Output:

True

Here, we directly accessed the is_leap_year attribute of the pandas date sample_date which returned True because the year 2016 was indeed a leap year.

Let’s now look at some examples of using this same attribute to check whether each year in a datetime column is leap year or not. First, we will create a sample dataframe –

# create a dataframe
matches_df = pd.DataFrame({
    "Date": ["2007-09-14", "2007-09-24", "2012-09-30", "2014-03-21", "2016-03-19", "2021-10-24"],
    "Venue": ["Durban, SA", "Johannesburg, SA", "Colombo, SL", "Dhaka, BAN", "Kolkata, IND", "Dubai, UAE"],
    "Winner": ["India", "India", "India", "India", "India", "Pakistan"]
})
# display the dataframe
print(matches_df)

Output:

         Date             Venue    Winner
0  2007-09-14        Durban, SA     India
1  2007-09-24  Johannesburg, SA     India
2  2012-09-30       Colombo, SL     India
3  2014-03-21        Dhaka, BAN     India
4  2016-03-19      Kolkata, IND     India
5  2021-10-24        Dubai, UAE  Pakistan

We now have a dataframe containing the Date, Venue, and the Winner of all the India v/s Pakistan encounters in T20 Cricket World Cup history.

Let’s find out which of these matches happened in a leap year. But first, we’ll have to convert the “Date” column to pandas datetime.

# convert "Date" to pandas datetime
matches_df["Date"] = pd.to_datetime(matches_df["Date"])
# display the dataframe
print(matches_df)

Output:

        Date             Venue    Winner
0 2007-09-14        Durban, SA     India
1 2007-09-24  Johannesburg, SA     India
2 2012-09-30       Colombo, SL     India
3 2014-03-21        Dhaka, BAN     India
4 2016-03-19      Kolkata, IND     India
5 2021-10-24        Dubai, UAE  Pakistan

Now, to access the is_leap_year attribute for each date, we’ll use .dt.is_leap_year and store the results to a new column.

# add a column of boolean is_leap_year
matches_df["LeapYear"] = matches_df["Date"].dt.is_leap_year
# display the dataframe
print(matches_df)

Output:

        Date             Venue    Winner  LeapYear
0 2007-09-14        Durban, SA     India     False
1 2007-09-24  Johannesburg, SA     India     False
2 2012-09-30       Colombo, SL     India      True
3 2014-03-21        Dhaka, BAN     India     False
4 2016-03-19      Kolkata, IND     India      True
5 2021-10-24        Dubai, UAE  Pakistan     False

You can see that matches in 2012 and 2016 happened in a leap year.

With this, we come to the end of this tutorial. The code examples and results presented in this tutorial have been implemented in a Jupyter Notebook with a python (version 3.8.3) kernel having pandas version 1.0.5


Subscribe to our newsletter for more informative guides and tutorials.
We do not spam and you can opt out any time.


Leave a Reply

Your email address will not be published. Required fields are marked *