Python Pandas - Check whether the year is a leap year from the Period object

To check whether the year is a leap year from the Period object, use the period.is_leap_year property. This property returns True if the year in the Period is a leap year, otherwise False.

Syntax

period.is_leap_year

Creating Period Objects

First, import pandas and create Period objects to work with ?

import pandas as pd

# Create Period objects with different years
period1 = pd.Period("2020-09-23 05:55:30")
period2 = pd.Period(freq="Y", year=2021, month=7, day=16, hour=2, minute=35)

# Display the Period objects
print("Period1...\n", period1)
print("Period2...\n", period2)
Period1...
 2020-09-23 05:55:30
Period2...
 2021

Checking for Leap Years

Use the is_leap_year property to determine if the year in each Period object is a leap year ?

import pandas as pd

# Create Period objects with different years
period1 = pd.Period("2020-09-23 05:55:30")
period2 = pd.Period(freq="Y", year=2021, month=7, day=16, hour=2, minute=35)

# Check whether the year is a leap year
res1 = period1.is_leap_year
res2 = period2.is_leap_year

print("Is the year in the 1st Period object a leap year?", res1)
print("Is the year in the 2nd Period object a leap year?", res2)
Is the year in the 1st Period object a leap year? True
Is the year in the 2nd Period object a leap year? False

Multiple Years Example

Here's an example checking multiple years to demonstrate the leap year logic ?

import pandas as pd

# Create Period objects for different years
years = [2020, 2021, 2024, 2100]
periods = [pd.Period(f"{year}-01-01") for year in years]

# Check leap years
for period in periods:
    print(f"Year {period.year}: {'Leap year' if period.is_leap_year else 'Not a leap year'}")
Year 2020: Leap year
Year 2021: Not a leap year
Year 2024: Leap year
Year 2100: Not a leap year

Key Points

  • The is_leap_year property works with any Period object regardless of frequency
  • Returns True for leap years (divisible by 4, except century years not divisible by 400)
  • Works with both string-based and parameter-based Period creation

Conclusion

The is_leap_year property provides a simple way to check if a Period object's year is a leap year. It returns a boolean value and works with any Period frequency or format.

Updated on: 2026-03-26T16:28:37+05:30

276 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements