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]

更新於: 2021 年 10 月 20 日

70 次瀏覽

開啟您的 職業生涯

透過完成課程獲得認證

立即開始
廣告
© . All rights reserved.