Cha*_*ron 11 python datetime python-2.7
我已经建立了一个功能,以获得当前季度的第一天和最后一天,但它有点长.我想知道,有没有更简洁的方法来完成这个?
我知道它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")
Run Code Online (Sandbox Code Playgroud)
编辑:如果这更适合代码审查,请告诉我
为什么要自己滚?
import pandas as pd
quarter_start = pd.to_datetime(pd.datetime.today() - pd.tseries.offsets.QuarterBegin(startingMonth=1)).date()
Run Code Online (Sandbox Code Playgroud)
为什么这么复杂:-)
from datetime import date
from calendar import monthrange
quarter = 2
year = 2016
first_month_of_quarter = 3 * quarter - 2
last_month_of_quarter = 3 * quarter
date_of_first_day_of_quarter = date(year, first_month_of_quarter, 1)
date_of_last_day_of_quarter = date(year, last_month_of_quarter, monthrange(year, last_month_of_quarter)[1])
Run Code Online (Sandbox Code Playgroud)
你可以这样做:
import bisect
import datetime as dt
def get_quarter_begin():
today = dt.date.today()
qbegins = [dt.date(today.year, month, 1) for month in (1,4,7,10)]
idx = bisect.bisect(qbegins, today)
return str(qbegins[idx-1])
Run Code Online (Sandbox Code Playgroud)
这解决了“第一种”情况;我将“最后一个”案例作为练习,但为了清晰起见,我建议将其保留为独立函数(对于您的原始版本,如果没有传递参数,会发生什么,这是非常奇怪的!)。
您不需要使用不必要的循环或大型库(例如 pandas)来执行此操作。您可以使用简单的整数除法/算术和日期时间库来完成此操作(尽管使用 dateutil 会产生更干净的代码)。
import datetime
def getQuarterStart(dt=datetime.date.today()):
return datetime.date(dt.year, (dt.month - 1) // 3 * 3 + 1, 1)
# using just datetime
def getQuarterEnd1(dt=datetime.date.today()):
nextQtYr = dt.year + (1 if dt.month>9 else 0)
nextQtFirstMo = (dt.month - 1) // 3 * 3 + 4
nextQtFirstMo = 1 if nextQtFirstMo==13 else nextQtFirstMo
nextQtFirstDy = datetime.date(nextQtYr, nextQtFirstMo, 1)
return nextQtFirstDy - datetime.timedelta(days=1)
# using dateutil
from dateutil.relativedelta import relativedelta
def getQuarterEnd2(dt=datetime.date.today()):
quarterStart = getQuarterStart(dt)
return quarterStart + relativedelta(months=3, days=-1)
Run Code Online (Sandbox Code Playgroud)
输出:
>>> d1=datetime.date(2017,2,15)
>>> d2=datetime.date(2017,1,1)
>>> d3=datetime.date(2017,10,1)
>>> d4=datetime.date(2017,12,31)
>>>
>>> getQuarterStart(d1)
datetime.date(2017, 1, 1)
>>> getQuarterStart(d2)
datetime.date(2017, 1, 1)
>>> getQuarterStart(d3)
datetime.date(2017, 10, 1)
>>> getQuarterStart(d4)
datetime.date(2017, 10, 1)
>>> getQuarterEnd1(d1)
datetime.date(2017, 3, 31)
>>> getQuarterEnd1(d2)
datetime.date(2017, 3, 31)
>>> getQuarterEnd1(d3)
datetime.date(2017, 12, 31)
>>> getQuarterEnd1(d4)
datetime.date(2017, 12, 31)
>>> getQuarterEnd2(d1)
datetime.date(2017, 3, 31)
>>> getQuarterEnd2(d2)
datetime.date(2017, 3, 31)
>>> getQuarterEnd2(d3)
datetime.date(2017, 12, 31)
>>> getQuarterEnd2(d4)
datetime.date(2017, 12, 31)
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
9644 次 |
| 最近记录: |