Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

pandas
data science
leap year
communitycreator

How to check whether a year is a leap year or not in Pandas

Onyejiaku Theophilus Chidalu

Overview

The .is_leap_year attribute of Pandas.Timestamp, which is an equivalent of Python's DateTime object, is used to check if a year is a leap year or not.

Syntax

The .is_leap_year attribute takes the syntax below:

Timestamp.is_leap_year
Syntax for the .is_leap_year attribute in Pandas

Parameter value

Since .is_leap_year is an attribute, it does not take a parameter value.

Return value

The .is_leap_year attribute returns a Boolean value indicating whether the year is a leap year (if Yes) or not (if False).

Example

# A code to illustrate the .is_leap_year attribute in Pandas
# importing the pandas library
import pandas as pd

# creating a Timestamp object
a = pd.Timestamp(2022, 4, 19)

# obtaining the number of days present in the month 4 (i.e. April)
print(a.is_leap_year)
Implementing the .is_leap_year attribute

Explanation

  • Line 3: We import the pandas module.
  • Line 6: We create a Timestamp object, a.
  • Line 9: We check whether the year (2022) in a is a leap year or not (from the output, False, the year is not a leap year), using the .is_leap_year attribute.

RELATED TAGS

pandas
data science
leap year
communitycreator

CONTRIBUTOR

Onyejiaku Theophilus Chidalu
RELATED COURSES

View all Courses

Keep Exploring