重新采样pandas datetimeindex以获得每个季度的第二个星期三

时间:2017-08-06 23:46:32

标签: pandas python-datetime

我有一个pandas日期时间范围:

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

我想重新取样以获得每个季度的第二个星期三,但我很难搞清楚这一点。

有人能够提供帮助吗?

1 个答案:

答案 0 :(得分:3)

确定自季度末和周三以来是否在7天内超过14天。

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)