2013-07-03 61 views
0

使用python,pandas或dateutil,我如何得到三個月期間的最後一個星期五,其中期末可能是四月。由於我的目標是在三個月交付週期的最後一個星期五的「焦點日期」,所以我可能需要將交貨時間延長到六個月。使用python如何獲取按季度的日期

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

回答

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

哪個現在是OP要求的一個季度(三個月期間)的最後一個星期五? – Alfe

+0

@falsetru,5/15/2013的輸出是錯誤的,第一個日期應該是jun/21而不是5/17。 – Merlin

+0

@默林,對不起,我不明白你的意思。我的回答/輸出不包含'jun/21'和'may/27'。 – falsetru

0

如果像我這樣的人來自谷歌在這裏結束了,我發現這種方法更容易閱讀:

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

pandas doc

相關問題