Python | Pandas Series.dt.is_leap_year

Series.dt can be used to access series values ​​as datetimelike and return multiple properties. Series.dt.is_leap_year Pandas Series.dt.is_leap_year returns a boolean indicator if the date is in a leap year.

Syntax: Series.dt.is_leap_year

Parameter: None

Returns: numpy array

Example # 1: Use the Series.dt.is_leap_year attribute to check if dates in the underlying data of this series object by leap year.

# import pandas as pd

import pandas as pd

  
# Create series

sr = pd.Series ([ ` 2012-12-31` , `2019-1-1 12: 30` , `2008-02-2 10:30` ,

`2010-1-1 09:25` , `2019-12-31 00:00` ])

  
# Create index

idx = [ `Day 1` , `Day 2` , ` Day 3` , `Day 4` , ` Day 5 ` ]

  
#install search index

sr.index = idx

 
# Convert baseline data to date and time

sr = pd.to_datetime (sr)

 
# Print series

print (sr)

Output:

We will now use the Series.dt.is_leap_year attribute to check if the dates in this series object are in a leap year.

#check, e If dates are given
# belongs to a leap year.

result = sr.dt.is_leap_year

 
# print the result

print (result)

Output:

As we can see from the output, the attribute is Series.dt.is_leap_year successfully accessed and returned a Boolean indicating whether the dates in this series object are in a leap year.

Example # 2: Use the Series.dt.is_leap_year attribute to check if the dates in the underlying data for this series object are in a leap year.

# import pandas as pd

import pandas as pd

 
# Create a series

sr = pd.Series (pd.date_range ( `2012-12-31 00:00` , periods = 5 , freq = `D` ))

 
# Create index

idx = [ `Day 1` , `Day 2` , < code class = "string"> `Day 3` , ` Day 4` , ` Day 5` ]

 
# set index

sr.index = idx

 
# Print series

print (sr)

Output:

We will now use the Series.dt.is_leap_year attribute to check if the dates in the given object of the series are in a leap year.

# check if dates are given
# belongs to leap year.

result = sr.dt.is_leap_year

 
# print the result

print (result)

Output:

As we can see from the output, the attribute Series.dt.is_leap_year successfully accessed and returned Boolean values ​​indicating whether the dates in this series object are in a leap year.