0

使用 python、pandas 或 dateutil,我如何获得三个月期间的最后一个星期五,期间结束可能是 4 月。由于我的目标是“重点日期”,即每个月的最后一个星期五,提前三个月,我可能需要将提前期延长至六个月。

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

我试过 pandasBMonthEndBQuarterEndWeekOfMonth(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)]
4

2 回答 2

2
>>> 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)]

规则

于 2013-07-03T13:04:44.787 回答
0

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

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

熊猫医生

于 2017-11-01T19:29:44.547 回答