Python Pandas - 指示 PeriodIndex 对象中的日期是否属于闰年

要指示 PeriodIndex 对象中的日期是否属于闰年,请使用periodIndex.is_leap_year属性。

首先,导入所需的库 -

import pandas as pd

创建一个 PeriodIndex 对象 -

periodIndex = pd.PeriodIndex(['2021-09-25 07:50:35', '2019-10-30 04:35:45',
'2016-07-15 02:25:15', '2020-06-25 09:20:55'], freq="T")

显示 PeriodIndex 对象 -

print("PeriodIndex...\n", periodIndex)

检查日期是否为闰年 -

print("\nWhether the date is a leap year?\n", periodIndex.is_leap_year)

示例

以下是代码 -

import pandas as pd

# Create a PeriodIndex object
# PeriodIndex is an immutable ndarray holding ordinal values indicating regular periods in time
# We have set the frequency using the "freq" parameter
periodIndex = pd.PeriodIndex(['2021-09-25 07:50:35', '2019-10-30 04:35:45',
'2016-07-15 02:25:15', '2020-06-25 09:20:55'], freq="T")

# Display PeriodIndex object
print("PeriodIndex...\n", periodIndex)

# Display PeriodIndex frequency
print("\nPeriodIndex frequency object...\n", periodIndex.freq)

# Display PeriodIndex frequency as string
print("\nPeriodIndex frequency object as a string...\n", periodIndex.freqstr)

# Check whether the date is a leap year
print("\nWhether the date is a leap year?\n", periodIndex.is_leap_year)
输出结果

这将产生以下代码 -

PeriodIndex...
PeriodIndex(['2021-09-25 07:50', '2019-10-30 04:35', '2016-07-15 02:25','2020-06-25 09:20'],
dtype='period[T]')

PeriodIndex frequency object...
<Minute>

PeriodIndex frequency object as a string...
T

Whether the date is a leap year?
[False False True True]

猜你喜欢