Python Pandas - 创建一个 PeriodIndex 并设置频率
要创建一个 PeriodIndex,请使用 pandas.PeriodIndex() 方法。要设置频率,请使用 freq 参数。
首先,导入所需的库 −
import pandas as pd
创建一个 PeriodIndex 对象。PeriodIndex 是一个不可变的 ndarray,它保存指示时间中规则日期的序数。我们使用“freq”参数设置了频率 −
periodIndex = pd.PeriodIndex(['2021-09-25', '2020-10-30', '2020-11-20'], freq="D")
显示 PeriodIndex 对象 −
print("PeriodIndex...\n", periodIndex)示例
以下是代码 −
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', '2020-10-30', '2020-11-20'], freq="D")
# Display PeriodIndex object
print("PeriodIndex...\n", periodIndex)
# Display day from the PeriodIndex object
print("\nThe number of days from the PeriodIndex...\n", periodIndex.day)输出
这将产生以下代码 −
PeriodIndex... PeriodIndex(['2021-09-25', '2020-10-30', '2020-11-20'], dtype='period[D]') The number of days from the PeriodIndex... Int64Index([25, 30, 20], dtype='int64')
广告
数据结构
网络
RDBMS
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP