I have the following table of objects
| OBJECTID | Conc | ID | start | finish | DateRange | Cat |
| 2 | ASPELBJNMI | LAPMT | 27/06/2018 | 11/8/2018 | 100 | 0 |
| 9 | ASPELBJNMI | KLKIY | 22/08/2018 | 31/08/2018 | 100 | 0 |
| 16 | ASPELBJNMI | HDOKK | 12/8/2018 | 30/09/2018 | 100 | 0 |
| 23 | ASPELBJNMI | XMCZW | 22/08/2018 | 20/10/2018 | 100 | 0 |
| 6 | BKRKAVQTZC | FCBES | 1/7/2018 | 15/08/2018 | 100 | 0 |
| 13 | BKRKAVQTZC | NHQGW | 24/08/2018 | 4/9/2018 | 100 | 0 |
| 20 | BKRKAVQTZC | LVUSS | 9/8/2018 | 4/10/2018 | 100 | 0 |
| 27 | BKRKAVQTZC | RQVNN | 18/08/2018 | 24/10/2018 | 100 | 0 |
| 7 | FKKTHEJGCY | IHORM | 2/7/2018 | 20/08/2018 | 100 | 0 |
| 14 | FKKTHEJGCY | AZFEV | 28/08/2018 | 8/9/2018 | 100 | 0 |
| 21 | FKKTHEJGCY | YAVOY | 4/8/2018 | 29/09/2018 | 100 | 0 |
| 28 | FKKTHEJGCY | AMWAX | 16/08/2018 | 22/10/2018 | 100 | 0 |
| 5 | JUNRNEXCRG | QCOWV | 30/06/2018 | 25/08/2018 | 100 | 0 |
| 12 | JUNRNEXCRG | AOYNL | 25/08/2018 | 9/9/2018 | 100 | 0 |
| 19 | JUNRNEXCRG | CGCHK | 4/8/2018 | 3/10/2018 | 100 | 0 |
| 26 | JUNRNEXCRG | IQYGJ | 12/8/2018 | 26/10/2018 | 100 | 0 |
I need to recursively run through calculating the min start date from 'start ' for a group of 'Conc' and finding the 'DateRange' by subtracting the min start date from 'finish'. Every time the DateRage becomes equal or less than 60 days, the Cat should increase by 1 for each group of Concs. Consequent loop should then calculate a new min start date. This should continue till when it is not possible to get a new DateRage for for a group of 'Conc. At the start, my code defaults DateRange to 100 and Cat to 0 at the start.
I have come up with the following code. It runs but seems to honor the outer loop on line 40 but exits without executing executing aspects of inner loop at line 54.
<SPAN class="keyword token">import</SPAN> arcpy
<SPAN class="keyword token">from</SPAN> datetime <SPAN class="keyword token">import</SPAN> datetime
<SPAN class="keyword token">from</SPAN> datetime <SPAN class="keyword token">import</SPAN> datetime <SPAN class="keyword token">as</SPAN> dt
<SPAN class="keyword token">from</SPAN> datetime <SPAN class="keyword token">import</SPAN> timedelta
<SPAN class="keyword token">import</SPAN> time
<SPAN class="keyword token">from</SPAN> datetime <SPAN class="keyword token">import</SPAN> date
<SPAN class="comment token">#Variables Start</SPAN>
arcpy<SPAN class="punctuation token">.</SPAN>env<SPAN class="punctuation token">.</SPAN>workspace <SPAN class="operator token">=</SPAN> r<SPAN class="string token">'E:\Classify\New File Geodatabase.gdb'</SPAN>
fc<SPAN class="operator token">=</SPAN> <SPAN class="string token">'Classifyjoin212'</SPAN>
field <SPAN class="operator token">=</SPAN><SPAN class="punctuation token">[</SPAN><SPAN class="string token">'Conc'</SPAN><SPAN class="punctuation token">,</SPAN><SPAN class="string token">'ID'</SPAN><SPAN class="punctuation token">]</SPAN>
fields <SPAN class="operator token">=</SPAN> <SPAN class="punctuation token">[</SPAN><SPAN class="string token">'start'</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="string token">'finish'</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="string token">'DateRange'</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="string token">'Cat'</SPAN><SPAN class="punctuation token">]</SPAN>
Classifyjoin <SPAN class="operator token">=</SPAN> <SPAN class="string token">"Classifyjoin"</SPAN>
Classifyjoin__2_ <SPAN class="operator token">=</SPAN> Classifyjoin
<SPAN class="comment token">#Def function</SPAN>
<SPAN class="keyword token">def</SPAN> <SPAN class="token function">min_cat</SPAN><SPAN class="punctuation token">(</SPAN>b<SPAN class="operator token">=</SPAN>datetime<SPAN class="punctuation token">.</SPAN>now<SPAN class="punctuation token">(</SPAN><SPAN class="punctuation token">)</SPAN> <SPAN class="punctuation token">,</SPAN> m<SPAN class="operator token">=</SPAN>datetime<SPAN class="punctuation token">.</SPAN>now<SPAN class="punctuation token">(</SPAN><SPAN class="punctuation token">)</SPAN> <SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">:</SPAN>
<SPAN class="string token">"""This function calculates the date range between earliest start in a category to the exact finish date of an individual member"""</SPAN><SPAN class="string token">""</SPAN>
<SPAN class="keyword token">return</SPAN><SPAN class="punctuation token">(</SPAN>b<SPAN class="operator token">-</SPAN>m<SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">.</SPAN>days
<SPAN class="comment token">#print min_cat(b=dt.strptime('2019-06-26', '%Y-%m-%d').date(), m=dt.strptime('2018-07-26', '%Y-%m-%d').date())</SPAN>
<SPAN class="comment token">#temp_lyr = arcpy.MakeFeatureLayer_management(fc, "temp_lyr") #If using a fc in gdb</SPAN>
temp_lyr<SPAN class="operator token">=</SPAN> arcpy<SPAN class="punctuation token">.</SPAN>MakeTableView_management<SPAN class="punctuation token">(</SPAN>fc<SPAN class="punctuation token">,</SPAN> <SPAN class="string token">"temp_lyr"</SPAN><SPAN class="punctuation token">)</SPAN>
listdates <SPAN class="operator token">=</SPAN> <SPAN class="punctuation token">[</SPAN><SPAN class="punctuation token">]</SPAN>
listCat <SPAN class="operator token">=</SPAN><SPAN class="punctuation token">[</SPAN><SPAN class="punctuation token">]</SPAN>
<SPAN class="comment token">#Variables end</SPAN>
ud <SPAN class="operator token">=</SPAN> <SPAN class="punctuation token">{</SPAN><SPAN class="punctuation token">}</SPAN>
<SPAN class="keyword token">with</SPAN> arcpy<SPAN class="punctuation token">.</SPAN>da<SPAN class="punctuation token">.</SPAN>SearchCursor<SPAN class="punctuation token">(</SPAN>fc<SPAN class="punctuation token">,</SPAN> field<SPAN class="punctuation token">)</SPAN> <SPAN class="keyword token">as</SPAN> rows<SPAN class="punctuation token">:</SPAN>
<SPAN class="keyword token">for</SPAN> row <SPAN class="keyword token">in</SPAN> rows<SPAN class="punctuation token">:</SPAN>
<SPAN class="keyword token">if</SPAN> row<SPAN class="punctuation token">[</SPAN><SPAN class="number token">0</SPAN><SPAN class="punctuation token">]</SPAN> <SPAN class="operator token">not</SPAN> <SPAN class="keyword token">in</SPAN> ud<SPAN class="punctuation token">:</SPAN>
ud<SPAN class="punctuation token">[</SPAN>row<SPAN class="punctuation token">[</SPAN><SPAN class="number token">0</SPAN><SPAN class="punctuation token">]</SPAN><SPAN class="punctuation token">]</SPAN> <SPAN class="operator token">=</SPAN> row<SPAN class="punctuation token">[</SPAN><SPAN class="number token">1</SPAN><SPAN class="punctuation token">]</SPAN>
<SPAN class="keyword token">for</SPAN> k <SPAN class="keyword token">in</SPAN> ud<SPAN class="punctuation token">:</SPAN>
where_clause<SPAN class="operator token">=</SPAN><SPAN class="string token">"Conc IN( '%s')"</SPAN> <SPAN class="operator token">%</SPAN> k <SPAN class="comment token">#+ "AND ID IN( '%s')" %v</SPAN>
<SPAN class="comment token">#print(where_clause)</SPAN>
kala <SPAN class="operator token">=</SPAN> arcpy<SPAN class="punctuation token">.</SPAN>SelectLayerByAttribute_management<SPAN class="punctuation token">(</SPAN>temp_lyr<SPAN class="punctuation token">,</SPAN> <SPAN class="string token">'NEW_SELECTION'</SPAN><SPAN class="punctuation token">,</SPAN> where_clause<SPAN class="punctuation token">)</SPAN><SPAN class="comment token">#Carry out the selection</SPAN>
cnt <SPAN class="operator token">=</SPAN> arcpy<SPAN class="punctuation token">.</SPAN>GetCount_management<SPAN class="punctuation token">(</SPAN>temp_lyr<SPAN class="punctuation token">)</SPAN> <SPAN class="comment token"># Counting the selected and copied features</SPAN>
<SPAN class="keyword token">print</SPAN><SPAN class="punctuation token">(</SPAN><SPAN class="string token">"The number of selected records is: "</SPAN> <SPAN class="operator token">+</SPAN> str<SPAN class="punctuation token">(</SPAN>cnt<SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">)</SPAN>
<SPAN class="keyword token">with</SPAN> arcpy<SPAN class="punctuation token">.</SPAN>da<SPAN class="punctuation token">.</SPAN>UpdateCursor<SPAN class="punctuation token">(</SPAN>kala<SPAN class="punctuation token">,</SPAN> fields<SPAN class="punctuation token">)</SPAN> <SPAN class="keyword token">as</SPAN> cursor<SPAN class="punctuation token">:</SPAN>
<SPAN class="keyword token">for</SPAN> row <SPAN class="keyword token">in</SPAN> cursor<SPAN class="punctuation token">:</SPAN>
<SPAN class="keyword token">if</SPAN> row<SPAN class="punctuation token">[</SPAN><SPAN class="number token">2</SPAN><SPAN class="punctuation token">]</SPAN><SPAN class="operator token">></SPAN><SPAN class="number token">60</SPAN><SPAN class="punctuation token">:</SPAN>
listdates<SPAN class="punctuation token">.</SPAN>append<SPAN class="punctuation token">(</SPAN>row<SPAN class="punctuation token">[</SPAN><SPAN class="number token">0</SPAN><SPAN class="punctuation token">]</SPAN><SPAN class="punctuation token">)</SPAN>
pg <SPAN class="operator token">=</SPAN>min<SPAN class="punctuation token">(</SPAN>listdates<SPAN class="punctuation token">)</SPAN><SPAN class="comment token">#.date()</SPAN>
jup <SPAN class="operator token">=</SPAN> <SPAN class="string token">'{}'</SPAN><SPAN class="punctuation token">.</SPAN>format<SPAN class="punctuation token">(</SPAN>pg<SPAN class="punctuation token">)</SPAN>
jop <SPAN class="operator token">=</SPAN> dt<SPAN class="punctuation token">.</SPAN>strptime<SPAN class="punctuation token">(</SPAN>jup<SPAN class="punctuation token">,</SPAN> <SPAN class="string token">'%Y-%m-%d %H:%M:%S'</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">.</SPAN>date<SPAN class="punctuation token">(</SPAN><SPAN class="punctuation token">)</SPAN>
bg <SPAN class="operator token">=</SPAN> <SPAN class="string token">'{}'</SPAN><SPAN class="punctuation token">.</SPAN>format<SPAN class="punctuation token">(</SPAN>row<SPAN class="punctuation token">[</SPAN><SPAN class="number token">1</SPAN><SPAN class="punctuation token">]</SPAN><SPAN class="punctuation token">)</SPAN>
job <SPAN class="operator token">=</SPAN> dt<SPAN class="punctuation token">.</SPAN>strptime<SPAN class="punctuation token">(</SPAN>bg<SPAN class="punctuation token">,</SPAN> <SPAN class="string token">'%Y-%m-%d %H:%M:%S'</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">.</SPAN>date<SPAN class="punctuation token">(</SPAN><SPAN class="punctuation token">)</SPAN>
a <SPAN class="operator token">=</SPAN>min_cat<SPAN class="punctuation token">(</SPAN>b<SPAN class="operator token">=</SPAN> job<SPAN class="punctuation token">,</SPAN> m<SPAN class="operator token">=</SPAN>jop<SPAN class="punctuation token">)</SPAN>
row<SPAN class="punctuation token">[</SPAN><SPAN class="number token">2</SPAN><SPAN class="punctuation token">]</SPAN> <SPAN class="operator token">=</SPAN> a
cursor<SPAN class="punctuation token">.</SPAN>updateRow<SPAN class="punctuation token">(</SPAN>row<SPAN class="punctuation token">)</SPAN>
<SPAN class="comment token">#continue</SPAN>
<SPAN class="keyword token">elif</SPAN> row<SPAN class="punctuation token">[</SPAN><SPAN class="number token">2</SPAN><SPAN class="punctuation token">]</SPAN><SPAN class="operator token"><=</SPAN><SPAN class="number token">60</SPAN><SPAN class="punctuation token">:</SPAN>
listCat<SPAN class="punctuation token">.</SPAN>append<SPAN class="punctuation token">(</SPAN>row<SPAN class="punctuation token">[</SPAN><SPAN class="number token">3</SPAN><SPAN class="punctuation token">]</SPAN><SPAN class="punctuation token">)</SPAN>
jp <SPAN class="operator token">=</SPAN>min<SPAN class="punctuation token">(</SPAN>listCat<SPAN class="punctuation token">)</SPAN>
kp <SPAN class="operator token">=</SPAN> int<SPAN class="punctuation token">(</SPAN><SPAN class="string token">'{}'</SPAN><SPAN class="punctuation token">.</SPAN>format<SPAN class="punctuation token">(</SPAN>jp<SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">)</SPAN>
kp <SPAN class="operator token">+=</SPAN><SPAN class="number token">1</SPAN>
row<SPAN class="punctuation token">[</SPAN><SPAN class="number token">3</SPAN><SPAN class="punctuation token">]</SPAN> <SPAN class="operator token">=</SPAN> kp
cursor<SPAN class="punctuation token">.</SPAN>updateRow<SPAN class="punctuation token">(</SPAN>row<SPAN class="punctuation token">)</SPAN>
<SPAN class="keyword token">continue</SPAN>
<SPAN class="comment token">#break</SPAN>
arcpy<SPAN class="punctuation token">.</SPAN>SelectLayerByAttribute_management<SPAN class="punctuation token">(</SPAN>temp_lyr<SPAN class="punctuation token">,</SPAN> <SPAN class="string token">"CLEAR_SELECTION"</SPAN><SPAN class="punctuation token">)</SPAN>
<SPAN class="comment token">#del row</SPAN>
<SPAN class="keyword token">del</SPAN> cursor
<SPAN class="keyword token">print</SPAN> <SPAN class="punctuation token">(</SPAN><SPAN class="string token">"finished"</SPAN><SPAN class="punctuation token">)</SPAN>
<SPAN class="line-numbers-rows"><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN></SPAN>The outcome with the code as it is; updates both the DateRange and cat ones.
Any other way around it?