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 #CreateaPeriodIndexobject #PeriodIndexisanimmutablendarrayholdingordinalvaluesindicatingregularperiodsintime # 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") #DisplayPeriodIndexobject print("PeriodIndex...\n", periodIndex) #DisplayPeriodIndexfrequency print("\nPeriodIndex frequency object...\n", periodIndex.freq) #DisplayPeriodIndexfrequency as string print("\nPeriodIndex frequency object as a string...\n", periodIndex.freqstr) #Checkwhetherthedateisaleapyear 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]