📜  Python| Pandas DatetimeIndex.is_leap_year

📅  最后修改于: 2022-05-13 01:54:40.419000             🧑  作者: Mango

Python| Pandas DatetimeIndex.is_leap_year

Python是一种用于进行数据分析的出色语言,主要是因为以数据为中心的Python包的奇妙生态系统。 Pandas就是其中之一,它使导入和分析数据变得更加容易。

如果日期属于闰年,Pandas DatetimeIndex.is_leap_year属性会返回一个布尔指示符。闰年是一个有 366 天(而不是 365 天)的年份,其中包括 2 月 29 日作为闰日。闰年是四的倍数,但能被 100 整除但不能被 400 整除的年份除外。

示例 #1:使用DatetimeIndex.is_leap_year属性检查 DatetimeIndex 对象中存在的日期是否属于闰年。

# importing pandas as pd
import pandas as pd
  
# Create the DatetimeIndex
didx = pd.DatetimeIndex(['2014-01-01', '2008-12-31', '2017-03-31', '2000-12-31'])
  
# Print the DatetimeIndex
print(didx)

输出 :

现在我们要查找给定 DatetimeIndex 对象中包含的日期是否属于闰年。

# find if the dates belong to leap year
didx.is_leap_year

输出 :

正如我们在输出中看到的,该函数返回了一个 numpy 数组,其中包含 DatetimeIndex 对象的每个条目的逻辑值。 True值表示对应日期属于闰年, False值表示对应日期不属于闰年。示例 #2:使用DatetimeIndex.is_leap_year属性检查 DatetimeIndex 对象中存在的日期是否属于闰年。

# importing pandas as pd
import pandas as pd
  
# Create the DatetimeIndex
didx = pd.date_range("2008-12-30", periods = 5, freq ='Q')
  
# Print the DatetimeIndex
print(didx)

输出 :

现在我们要查找给定 DatetimeIndex 对象中包含的日期是否属于闰年。

# find if the dates belong to leap year
didx.is_leap_year

输出 :

正如我们在输出中看到的,该函数返回了一个 numpy 数组,其中包含 DatetimeIndex 对象的每个条目的逻辑值。 True值表示对应日期属于闰年, False值表示对应日期不属于闰年。