2016-10-10 99 views
2

我試圖使用Pandas在Python中創建一個時間索引,其中包含對應於週期性發薪日的條目。具體來說,我希望索引對應於本月的第一個和第三個星期五。有人可以給出一個代碼片段來證明這一點嗎?使用熊貓創建Paydays的DateOffset

喜歡的東西:

import pandas as pd 
idx = pd.date_range("2016-10-10", periods=26, freq=<offset here?>) 
+0

我不知道大熊貓有這種內置的功能。您可能必須使用'dateutil'來構建一系列日期時間對象。請參閱:http://stackoverflow.com/questions/28680896/how-can-i-get-the-3rd-friday-of-a-month-in-python –

回答

2

試試這個:

In [6]: pd.date_range("2016-10-10", periods=26, freq='WOM-1FRI').union(pd.date_range("2016-10-10", periods=26, freq='WOM-3FRI')) 
Out[6]: 
DatetimeIndex(['2016-10-21', '2016-11-04', '2016-11-18', '2016-12-02', '2016-12-16', '2017-01-06', '2017-01-20', '2017-02-03', '2017-02-17', 
'2017-03-03', '2017-03-17', '2017-04-07', '2017-04-21', 
       '2017-05-05', '2017-05-19', '2017-06-02', '2017-06-16', '2017-07-07', '2017-07-21', '2017-08-04', '2017-08-18', '2017-09-01', 
'2017-09-15', '2017-10-06', '2017-10-20', '2017-11-03', 
       '2017-11-17', '2017-12-01', '2017-12-15', '2018-01-05', '2018-01-19', '2018-02-02', '2018-02-16', '2018-03-02', '2018-03-16', 
'2018-04-06', '2018-04-20', '2018-05-04', '2018-05-18', 
       '2018-06-01', '2018-06-15', '2018-07-06', '2018-07-20', '2018-08-03', '2018-08-17', '2018-09-07', '2018-09-21', '2018-10-05', 
'2018-10-19', '2018-11-02', '2018-11-16', '2018-12-07'], 
       dtype='datetime64[ns]', freq=None) 
+0

光滑,這工作。謝謝! – themaestro

+0

@themaestro,歡迎您:) – MaxU