我正在尝试从季度数中计算月份,季度细节

示例:当前日期= 2017年4月29日

输入/输出:

No_of_quarters=6


O / p:

Quarters = ["Q1-2016","Q2-2016", "Q3-2016", "Q4-2016", "Q1-2017", "Q2-2017"]
Months = ["Jan-2016","Feb-2016","Mar-2016","Apr-2016","May-2016","Jun-2016","Jul-2016","Aug-2016","Sep-2016","Oct-2016","Nov-2016","Dec-2016","Jan-2017","Feb-2017","Mar-2017","Apr-2017"]


我可以重用任何预定义的功能吗?或者我需要使用以下方式进行计算

today = datetime.date.today()
month = int(today.strftime("%m")) - 1
quarter = month / 3 + 1
quarter_Name = "Q" + str(quarter)  + "-" +  today.strftime("%Y")
month_name = today.strftime("%b") + "-" +  today.strftime("%Y")

最佳答案

使用熊猫和no_of_quarters输入

import pandas as pd
import datetime

No_of_quarters=6

today = datetime.date.today()
month = pd.Period(today, freq='M')
quarter = month.asfreq('Q-DEC')

print "Quarters:"
for x in range (0,No_of_quarters-1):
    print quarter-x

print "Months:"
for x in range (0,(No_of_quarters*3)-1):
    print month-x

关于python - Python:根据季度数计算月份,季度详细信息,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/43712792/

10-12 21:56