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.
Using Pandas.Timestamp.is_leap_year
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.
Check if each year in a Pandas Column is Leap Year or Not
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.
Introductory ⭐
- Harvard University Data Science: Learn R Basics for Data Science
- Standford University Data Science: Introduction to Machine Learning
- UC Davis Data Science: Learn SQL Basics for Data Science
- IBM Data Science: Professional Certificate in Data Science
- IBM Data Analysis: Professional Certificate in Data Analytics
- Google Data Analysis: Professional Certificate in Data Analytics
- IBM Data Science: Professional Certificate in Python Data Science
- IBM Data Engineering Fundamentals: Python Basics for Data Science
Intermediate ⭐⭐⭐
- Harvard University Learning Python for Data Science: Introduction to Data Science with Python
- Harvard University Computer Science Courses: Using Python for Research
- IBM Python Data Science: Visualizing Data with Python
- DeepLearning.AI Data Science and Machine Learning: Deep Learning Specialization
Advanced ⭐⭐⭐⭐⭐
- UC San Diego Data Science: Python for Data Science
- UC San Diego Data Science: Probability and Statistics in Data Science using Python
- Google Data Analysis: Professional Certificate in Advanced Data Analytics
- MIT Statistics and Data Science: Machine Learning with Python - from Linear Models to Deep Learning
- MIT Statistics and Data Science: MicroMasters® Program in Statistics and Data Science
🔎 Find Data Science Programs 👨💻 111,889 already enrolled
Disclaimer: Data Science Parichay is reader supported. When you purchase a course through a link on this site, we may earn a small commission at no additional cost to you. Earned commissions help support this website and its team of writers.
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.