2016-07-11 40 views
0

更大的第一次约会,我想拿到第一周一月比7月10日更大的日期列表,我想知道是否有避免for循环的优雅的解决方案/列表理解。 这里是我到目前为止的代码,让超过10件的所有七月星期一:周大于x

import pandas as pd 
last_date = '08-Jul-2016' 
monday2_dates=pd.date_range('1-Jan-1999',last_date, freq='W-MON') 
g1=pd.DataFrame(1.0, columns=['dummy'], index=monday2_dates) 
g1=g1.loc[(g1.index.month==7) & (g1.index.day>=10)] 
+0

http://stackoverflow.com/questions/6558535/python-find-the-date-for-the-first-monday-after-a-given-a-date – Tim

回答

1

IIUC你能做到这样:指定日期范围内第2个星期一的

获取列表

In [116]: rng = pd.date_range('1-Jan-1999',last_date, freq='WOM-2MON') 

筛选,以便我们将只有在七月那些day >= 10

In [117]: rng = rng[(rng.month==7) & (rng.day >= 10)] 

创建相应的DF

In [118]: df = pd.DataFrame({'dummy':[1] * len(rng)}, index=rng) 

In [119]: df 
Out[119]: 
      dummy 
1999-07-12  1 
2000-07-10  1 
2003-07-14  1 
2004-07-12  1 
2005-07-11  1 
2006-07-10  1 
2008-07-14  1 
2009-07-13  1 
2010-07-12  1 
2011-07-11  1 
2014-07-14  1 
2015-07-13  1 
+0

@ NickD1,有帮助吗? – MaxU