📜  Python| pandas.period_range() 方法

📅  最后修改于: 2022-05-13 01:54:19.253000             🧑  作者: Mango

Python| pandas.period_range() 方法

Python是一种用于进行数据分析的出色语言,主要是因为以数据为中心的Python包的奇妙生态系统。 Pandas 就是其中之一,它使导入和分析数据变得更加容易。

pandas.period_range()是 Pandas 中的通用函数之一,用于返回固定频率 PeriodIndex,默认频率为 day(日历)。

代码#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)

输出: