我已经建立了一个功能,以获得当前季度的第一天和最后一天,但它有点长.我想知道,有没有更简洁的方法来完成这个?
我知道pandas有一个QuarterBegin()函数,但我无法以更简洁的方式实现它.
import datetime as dt
from dateutil.relativedelta import relativedelta
def get_q(first=None,last=None):
today = dt.date.today()
qmonth = [1, 4, 7, 10]
if first:
for i,v in enumerate(qmonth):
if (today.month-1)//3 == i:
return dt.date(today.year,qmonth[i],1).strftime("%Y-%m-%d")
if last:
firstday = dt.datetime.strptime(get_q(first=True),"%Y-%m-%d")
lastday = firstday + relativedelta(months=3, days=-1)
return lastday.strftime("%Y-%m-%d")
编辑:如果这更适合Code Review,请告诉我