使用python如何按季度获取日期

时间:2013-07-03 12:54:09

标签: python pandas python-dateutil

使用python,pandas或dateutil,如何获得三个月期间的最后一个星期五,期末可能是四月。由于我的目标是“焦点日期”,即上个星期五的三个月交付时间,我可能需要将提前期延长至六个月。

SourceDate, Q1, Q2, Q3

2013-Jun-26 2013-Sep-20 2013-Aug-16 2013-Jul-19
2013-Jun-25 2013-Sep-20 2013-Aug-16 2013-Jul-19
2013-Jun-24 **2013-Sep-20** 2013-Aug-16 2013-Jul-19
**2013-Jun-21** **2013-Jun-21** 2013-Aug-16 2013-Jul-19
2013-Jun-20 2013-Jun-21 2013-Aug-16 2013-Jul-19
2013-Jun-19 2013-Jun-21 2013-Aug-16 2013-Jul-19
2013-Jun-18 2013-Jun-21 2013-Aug-16 2013-Jul-19
2013-Jun-17 2013-Jun-21 2013-Aug-16 2013-Jul-19
2013-Jun-14 2013-Jun-21 2013-Aug-16 2013-Jul-19
2013-Jun-13 2013-Jun-21 2013-Aug-16 2013-Jul-19
2013-Jun-12 2013-Jun-21 2013-Aug-16 2013-Jul-19
2013-Jun-11 2013-Jun-21 2013-Aug-16 2013-Jul-19
2013-Jun-10 2013-Jun-21 2013-Aug-16 2013-Jul-19
2013-Jun-07 2013-Jun-21 2013-Aug-16 2013-Jul-19
2013-Jun-06 2013-Jun-21 2013-Aug-16 2013-Jul-19
2013-Jun-05 2013-Jun-21 2013-Aug-16 2013-Jul-19
2013-Jun-04 2013-Jun-21 2013-Aug-16 2013-Jul-19
2013-Jun-03 2013-Jun-21 2013-Aug-16 2013-Jul-19
2013-May-31 2013-Jun-21 2013-Aug-16 2013-Jul-19
2013-May-30 2013-Jun-21 2013-Aug-16 2013-Jul-19
2013-May-29 2013-Jun-21 2013-Aug-16 2013-Jul-19
2013-May-28 2013-Jun-21 2013-Aug-16 2013-Jul-19
2013-May-24 2013-Jun-21 2013-Aug-16 2013-Jul-19
2013-May-23 2013-Jun-21 2013-Aug-16 2013-Jul-19
2013-May-22 2013-Jun-21 2013-Aug-16 2013-Jul-19
2013-May-21 2013-Jun-21 2013-Aug-16 2013-Jul-19
2013-May-20 2013-Jun-21 **2013-Aug-16** 2013-Jul-19
**2013-May-17** 2013-Jun-21 **2013-May-17** 2013-Jul-19
2013-May-16 2013-Jun-21 2013-May-17 2013-Jul-19
2013-May-15 2013-Jun-21 2013-May-17 2013-Jul-19
2013-May-14 2013-Jun-21 2013-May-17 2013-Jul-19
2013-May-13 2013-Jun-21 2013-May-17 2013-Jul-19
2013-May-10 2013-Jun-21 2013-May-17 2013-Jul-19
2013-May-09 2013-Jun-21 2013-May-17 2013-Jul-19
2013-May-08 2013-Jun-21 2013-May-17 2013-Jul-19
2013-May-07 2013-Jun-21 2013-May-17 2013-Jul-19
2013-May-06 2013-Jun-21 2013-May-17 2013-Jul-19
2013-May-03 2013-Jun-21 2013-May-17 2013-Jul-19
2013-May-02 2013-Jun-21 2013-May-17 2013-Jul-19
2013-May-01 2013-Jun-21 2013-May-17 2013-Jul-19
2013-Apr-30 2013-Jun-21 2013-May-17 2013-Jul-19
2013-Apr-29 2013-Jun-21 2013-May-17 2013-Jul-19
2013-Apr-26 2013-Jun-21 2013-May-17 2013-Jul-19
2013-Apr-25 2013-Jun-21 2013-May-17 2013-Jul-19
2013-Apr-24 2013-Jun-21 2013-May-17 2013-Jul-19
2013-Apr-23 2013-Jun-21 2013-May-17 2013-Jul-19
2013-Apr-22 2013-Jun-21 2013-May-17 **2013-Jul-19**
**2013-Apr-19** 2013-Jun-21 2013-May-17 **2013-Apr-19**
2013-Apr-18 2013-Jun-21 2013-May-17 2013-Apr-19
2013-Apr-17 2013-Jun-21 2013-May-17 2013-Apr-19
2013-Apr-16 2013-Jun-21 2013-May-17 2013-Apr-19
2013-Apr-15 2013-Jun-21 2013-May-17 2013-Apr-19
2013-Apr-12 2013-Jun-21 2013-May-17 2013-Apr-19
2013-Apr-11 2013-Jun-21 2013-May-17 2013-Apr-19
2013-Apr-10 2013-Jun-21 2013-May-17 2013-Apr-19
2013-Apr-09 2013-Jun-21 2013-May-17 2013-Apr-19

我已经尝试过将BMonthEndBQuarterEndWeekOfMonth(weekday = 4, week =2)一起尝试过,我不知道滚动的部分。

编辑:

>>>d = dt.datetime(2013, 5, 15)
>>>list(rrule(MONTHLY, count=5, byweekday=FR, bysetpos=3,dtstart =d ))
[datetime.datetime(2013, 5, 17, 0, 0),
 datetime.datetime(2013, 6, 21, 0, 0),
 datetime.datetime(2013, 7, 19, 0, 0),
 datetime.datetime(2013, 8, 16, 0, 0),
 datetime.datetime(2013, 9, 20, 0, 0)]

2 个答案:

答案 0 :(得分:3)

>>> from dateutil.rrule import *
>>> list(rrule(MONTHLY, count=3, byweekday=FR, bysetpos=-1))
[datetime.datetime(2013, 7, 26, 22, 4, 20),
 datetime.datetime(2013, 8, 30, 22, 4, 20),
 datetime.datetime(2013, 9, 27, 22, 4, 20)]

>>> list(rrule(MONTHLY, count=12, byweekday=FR, bysetpos=-1))[::3]
[datetime.datetime(2013, 7, 26, 22, 15, 14),
 datetime.datetime(2013, 10, 25, 22, 15, 14),
 datetime.datetime(2014, 1, 31, 22, 15, 14),
 datetime.datetime(2014, 4, 25, 22, 15, 14)]

rrule

答案 1 :(得分:0)

如果像我这样的人最终来自谷歌,我发现这种方法更容易阅读:

d = datetime.now()
import pandas.tseries.offsets as offsets
print(d + offsets.QuarterEnd() - offsets.Week(weekday=4))

pandas doc