Python Pandas PeriodIndex.asfreq
Python是一种进行数据分析的伟大语言,主要是因为以数据为中心的Python包的奇妙生态系统。Pandas就是这些包中的一个,它使导入和分析数据变得更加容易。
Pandas PeriodIndex.asfreq()函数将给定的PeriodIndex对象的每个元素转换为指定的频率。
语法 : PeriodIndex.asfreq(freq=None, how=’E’)
参数 :
freq :一个频率
how:‘E’、’END’或’FINISH’表示结束,’S’、’START’或’BEGIN’表示开始。
返回:带有新频率的PeriodIndex。
示例#1:使用PeriodIndex.asfreq()函数将给定的PeriodIndex对象的频率转换成指定的时间序列频率。
# importing pandas as pd
import pandas as pd
# Create the PeriodIndex object
pidx = pd.PeriodIndex(start ='2004-11-11 02:45:21 ',
end ='2021-5-21 8:45:29', freq ='Y')
# Print the PeriodIndex object
print(pidx)
输出 :

现在我们将使用PeriodIndex.asfreq()函数将给定的PeriodIndex对象的时间序列频率转换为指定频率。
# convert the frequency
# 'M' stands for monthly frequency
pidx.asfreq('M')
输出 :

正如我们在输出中看到的,PeriodIndex.asfreq()函数已经将给定的PeriodIndex对象的频率转换为指定的频率。
示例#2:使用PeriodIndex.asfreq()函数将给定的PeriodIndex对象的频率转换为指定的时间序列频率。
# importing pandas as pd
import pandas as pd
# Create the PeriodIndex object
pidx = pd.PeriodIndex(start ='2016-10-12 11:12:02',
end ='2020-04-12 11:32:12', freq ='Q')
# Print the PeriodIndex object
print(pidx)
输出 :

现在我们将使用PeriodIndex.asfreq()函数将给定的PeriodIndex对象的时间序列频率转换为指定频率。
# convert the frequency
# 'Y' stands for yearly frequency
pidx.asfreq('Y')
输出 :

正如我们在输出中看到的,PeriodIndex.asfreq()函数已经将给定的PeriodIndex对象的频率转换为指定的频率。
极客教程