Python Pandas - 创建一个 PeriodIndex 并获取该周期的天数
要创建PeriodIndex,请使用方法。使用PeriodIndex.day属性获取期间的天数。pandas.PeriodIndex()
首先,导入所需的库-
import pandas as pd
创建一个PeriodIndex对象。PeriodIndex是一个不可变的ndarray,其中包含指示定期时间段的序数值。我们使用“freq”参数设置了频率-
periodIndex = pd.PeriodIndex(['2018-07-25', '2019-10-30', '2020-11-20', '2021-09-15', '2022-03-12', '2023-06-18'], freq="D")
显示PeriodIndex对象-
print("PeriodIndex...\n", periodIndex)从PeriodIndex对象显示日期-
print("\nThe number of days from the PeriodIndex...\n", periodIndex.day)示例
以下是代码-
import pandas as pd
#CreateaPeriodIndexobject
#PeriodIndexisanimmutablendarrayholdingordinalvaluesindicatingregularperiodsintime
# We have set the frequency using the "freq" parameter
periodIndex = pd.PeriodIndex(['2018-07-25', '2019-10-30', '2020-11-20',
'2021-09-15', '2022-03-12', '2023-06-18'], freq="D")
#DisplayPeriodIndexobject
print("PeriodIndex...\n", periodIndex)
#DisplayPeriodIndexfrequency
print("\nPeriodIndex frequency...\n", periodIndex.freq)
#DisplaydayfromthePeriodIndexobject
print("\nThe number of days from the PeriodIndex...\n", periodIndex.day)输出结果这将产生以下代码-
PeriodIndex... PeriodIndex(['2018-07-25', '2019-10-30', '2020-11-20', '2021-09-15', '2022-03-12', '2023-06-18'], dtype='period[D]') PeriodIndex frequency... <Day> The number of days from the PeriodIndex... Int64Index([25, 30, 20, 15, 12, 18], dtype='int64')
热门推荐
10 三八简短祝福语给婆婆
11 男朋友新年祝福语简短
12 口才课期末祝福语简短
13 婆婆高寿祝福语大全简短
14 中考祝福语简短卡片大全
15 手写母亲的祝福语简短
16 新年过后奋斗祝福语简短
17 怎么说祝福语简短
18 开学新人祝福语简短英语