Python Pandas Series.dt.is_leap_year

Python Pandas Series.dt.is_leap_year

Series.dt可以用来访问类似于数据时间的系列的值,并返回几个属性。Pandas Series.dt.is_leap_year属性返回一个布尔指标,如果该日期属于闰年。

语法: Series.dt.is_leap_year

参数:

返回 : numpy数组

例子#1:使用Series.dt.is_leap_year属性来检查给定系列对象的基础数据中的日期是否属于闰年。

# importing pandas as pd
import pandas as pd
  
# Creating the 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'])
  
# Creating the index
idx = ['Day 1', 'Day 2', 'Day 3', 'Day 4', 'Day 5']
  
# set the index
sr.index = idx
  
# Convert the underlying data to datetime 
sr = pd.to_datetime(sr)
  
# Print the series
print(sr)

输出 :

Python Pandas Series.dt.is_leap_year

现在我们将使用Series.dt.is_leap_year属性来检查给定系列对象中的日期是否属于闰年。

# check if dates given
# belongs to a leap year.
result = sr.dt.is_leap_year
  
# print the result
print(result)

输出 :

Python Pandas Series.dt.is_leap_year

正如我们在输出中看到的,Series.dt.is_leap_year属性已经成功访问并返回布尔值,表明给定系列对象中的日期是否属于闰年。

示例#2 :使用Series.dt.is_leap_year属性来检查给定系列对象的基础数据中的日期是否属于闰年。

# importing pandas as pd
import pandas as pd
  
# Creating the Series
sr = pd.Series(pd.date_range('2012-12-31 00:00', periods = 5, freq = 'D'))
  
# Creating the index
idx = ['Day 1', 'Day 2', 'Day 3', 'Day 4', 'Day 5']
  
# set the index
sr.index = idx
  
# Print the series
print(sr)

输出 :

Python Pandas Series.dt.is_leap_year

现在我们将使用Series.dt.is_leap_year属性来检查给定系列对象中的日期是否属于闰年。

# check if dates given
# belongs to a leap year.
result = sr.dt.is_leap_year
  
# print the result
print(result)

输出 :

Python Pandas Series.dt.is_leap_year

正如我们在输出中看到的,Series.dt.is_leap_year属性已经成功访问并返回布尔值,表明给定系列对象中的日期是否属于闰年。

Python教程

Java教程

Web教程

数据库教程

图形图像教程

大数据教程

开发工具教程

计算机教程