Python| pandas.period_range() 方法
Python是一种用于进行数据分析的出色语言,主要是因为以数据为中心的Python包的奇妙生态系统。 Pandas 就是其中之一,它使导入和分析数据变得更加容易。
pandas.period_range()
是 Pandas 中的通用函数之一,用于返回固定频率 PeriodIndex,默认频率为 day(日历)。
Syntax: pandas.to_numeric(arg, errors=’raise’, downcast=None)
Parameters:
start : Left bound for generating periods
end : Right bound for generating periods
periods : Number of periods to generate
freq : Frequency alias
name : Name of the resulting PeriodIndex
Returns: PeriodIndex
代码#1:
# importing pandas as pd
import pandas as pd
# period_range with freq = day
per1 = pd.period_range(start ='2018-12-20',
end ='2019-01-01', freq ='D')
# period_range with freq = month
per2 = pd.period_range(start ='2018-12-20',
end ='2019-12-01', freq ='M')
print(per1, "\n\n", per2)
输出:
代码#2:
# importing pandas as pd
import pandas as pd
# period_range with freq = day
per1 = pd.period_range(start ='2018-12-20',
end ='2019-01-01', freq ='D')
for val in per1:
print(val)
输出:
代码#3:
# importing pandas as pd
import pandas as pd
# Calling with pd.Period
per = pd.period_range(start = pd.Period('2017Q1', freq ='Q'),
end = pd.Period('2018Q2', freq ='Q'), freq ='M')
for val in per:
print(val)
输出: