Python Pandas - 从 PeriodIndex 对象返回频率对象作为字符串
要从 PeriodIndex 对象返回频率对象作为字符串,请使用 PeriodIndex.freqstr 属性。
首先,导入所需的库 −
import pandas as pd
创建一个 PeriodIndex 对象。PeriodIndex 是一个不可变的 ndarray,它保存指示时间中规律周期的序数 −
periodIndex = pd.PeriodIndex(['2021-09-25 07:50:35', '2019-10-30 04:35:45', '2021-07-15 02:25:15', '2022-06-25 07:20:55'], freq="T")
显示 PeriodIndex 对象 −
print("PeriodIndex...\n", periodIndex)
显示 PeriodIndex 频率为字符串 −
print("\nPeriodIndex frequency object as a string...\n", periodIndex.freqstr)
示例
以下为代码 −
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', '2021-07-15 02:25:15', '2022-06-25 07: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)
输出
产生以下代码 −
PeriodIndex... PeriodIndex(['2021-09-25 07:50', '2019-10-30 04:35', '2021-07-15 02:25', '2022-06-25 07:20'], dtype='period[T]') PeriodIndex frequency object... <Minute> PeriodIndex frequency object as a string... T
广告