w3resource

Pandas Series property: dt.is_leap_year

Series.dt.is_leap_year property

A leap year is a year, which has 366 days (instead of 365) including 29th of February as an intercalary day. Leap years are years which are multiples of four with the exception of years divisible by 100 but not by 400.

The is_leap_year property is used to check a given date belongs to a leap year or not.

Syntax:

Series.dt.is_leap_year
Pandas Series: dt.is_leap_year property

Returns: Series or ndarray
Booleans indicating if dates belong to a leap year.

Example:

Python-Pandas Code:

import numpy as np
import pandas as pd
idx = pd.date_range("2016-01-01", "2019-01-01", freq="Y")
idx

Output:

DatetimeIndex(['2016-12-31', '2017-12-31', '2018-12-31'], dtype='datetime64[ns]', freq='A-DEC')

Python-Pandas Code:

import numpy as np
import pandas as pd
idx = pd.date_range("2016-01-01", "2019-01-01", freq="Y")
idx.is_leap_year

Output:

array([ True, False, False])
Pandas Series: dt.is_leap_year property

Python-Pandas Code:

import numpy as np
import pandas as pd
dates_series = pd.Series(idx)
dates_series

Output:

0   2016-12-31
1   2017-12-31
2   2018-12-31
dtype: datetime64[ns]

Python-Pandas Code:

import numpy as np
import pandas as pd
dates_series = pd.Series(idx)
dates_series.dt.is_leap_year

Output:

0     True
1    False
2    False
dtype: bool

Previous: Series.dt.is_year_end property
Next: Series.dt.to_period property



Become a Patron!

Follow us on Facebook and Twitter for latest update.

It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.

https://w3resource.com/pandas/series/series-dt-is_leap_year.php