0

I would like to get the first monday in july that is greater than July 10th for a list of dates, and i am wondering if there's an elegant solution that avoids for loops/list comprehension. Here is my code so far that gives all july mondays greater than the 10th:

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)]
NickD1
  • 393
  • 1
  • 4
  • 14
  • http://stackoverflow.com/questions/6558535/python-find-the-date-for-the-first-monday-after-a-given-a-date – Tim Jul 11 '16 at 12:11

1 Answers1

1

IIUC you can do it this way:

get list of 2nd Mondays within specified date range

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

filter them so that we will have only those in July with day >= 10

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

create a corresponding 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
MaxU - stand with Ukraine
  • 205,989
  • 36
  • 386
  • 419