2017-08-06 79 views
2

我有一個熊貓日期時間範圍的第二個星期三:重新取樣大熊貓datetimeindex獲得每季度

start_date = '2001-01-01' 
end_date = '2017-08-03' 
index_dates = pd.date_range(start_date,end_date,freq='B') 

我想重新取樣本,以獲得每季度的第二個星期三,但我有很大的困難,搞清楚了這一點。

有人能幫忙嗎?

回答

3

確定它是否在季度末以及其週三以來的7天內。

fortnight = pd.Timedelta(14, unit='D') 
week = pd.Timedelta(7, unit='D') 
since_qe = index_dates - (index_dates - pd.offsets.QuarterEnd()) 
wed = index_dates.weekday == 2 

index_dates[(week < since_qe) & (since_qe < fortnight) & wed] 

DatetimeIndex(['2001-01-10', '2001-04-11', '2001-07-11', '2001-10-10', 
       '2002-01-09', '2002-04-10', '2002-07-10', '2002-10-09', 
       '2003-01-08', '2003-04-09', '2003-07-09', '2003-10-08', 
       '2004-10-13', '2005-01-12', '2005-04-13', '2005-07-13', 
       '2005-10-12', '2006-01-11', '2006-04-12', '2006-07-12', 
       '2006-10-11', '2007-01-10', '2007-04-11', '2007-07-11', 
       '2007-10-10', '2008-01-09', '2008-04-09', '2008-07-09', 
       '2008-10-08', '2009-04-08', '2009-07-08', '2010-01-13', 
       '2010-10-13', '2011-01-12', '2011-04-13', '2011-07-13', 
       '2011-10-12', '2012-01-11', '2012-04-11', '2012-07-11', 
       '2012-10-10', '2013-01-09', '2013-04-10', '2013-07-10', 
       '2013-10-09', '2014-01-08', '2014-04-09', '2014-07-09', 
       '2014-10-08', '2015-04-08', '2015-07-08', '2016-01-13', 
       '2016-04-13', '2016-07-13', '2016-10-12', '2017-01-11', 
       '2017-04-12', '2017-07-12'], 
       dtype='datetime64[ns]', freq=None)