Whether leap year or not in pandas python

is_leap_year is the inbuilt method to indicate whether a given year is leap year or not in Pandas Python. Let’s see how to

  • Get Whether the given year is leap year or not in pandas python

First lets create the dataframe

import pandas as pd
import numpy as np
import datetime

date1 = pd.Series(pd.date_range('2012-1-1 12:00:00', periods=7, freq='Y'))
df = pd.DataFrame(dict(date_given=date1))
print(df)

so the resultant dataframe will be

Whether leap year or not in pandas python 1

 

is_leap_year function gets whether the given year is leap year or not.

df['is_leap_year'] = df['date_given'].dt.is_leap_year
print(df)

so the resultant dataframe will be

Whether leap year or not in pandas python 2

 

Whether leap year or not in pandas python p                                                                                                           Whether leap year or not in pandas python n

Author

  • Sridhar Venkatachalam

    With close to 10 years on Experience in data science and machine learning Have extensively worked on programming languages like R, Python (Pandas), SAS, Pyspark.