Skip to content

Commit

Permalink
Adding functional For loop Try/Catch example code
Browse files Browse the repository at this point in the history
Hi Marcela, this is a working example of complex indexing that happens in the for loop and try blocks of the R SDG formatting code. I left in lines that helped me put the indexing together in pieces, and hopefully can help you understand the pieces of the long indexing call. These lines have "#FOR DEMO" written above them. It turns out the .item() calls at the end of some of the expressions are critical to prevent the mis-matched indexing errors we were getting earlier (they access the actual elements in the data structures returned by the expressions). The 'try' blocks are also essential I realized, because the lookup fails if a field collected sample has not yet been processed by the lab.
To incorporate this into the Python function, remove the #FOR DEMO lines, and add another indexing/assignment line with the correct column names substituted in the try block (so there should be a total of 2 assignment lines within each try block). Then make the 3 different try blocks like in the R code. Let me know if you have any questions!
  • Loading branch information
geohouse authored Jul 3, 2020
1 parent 2eaaa39 commit 734ff0e
Showing 1 changed file with 31 additions and 0 deletions.
31 changes: 31 additions & 0 deletions SDG_FORLOOP_WITHTRYCATCH_TEMPLATE.PY
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
for x in range(len(outputDF['waterSampleID'])):
# print(x)
# FOR DEMO
holder = outputDF.loc[outputDF.index[[x]], 'referenceAirSampleID']
# FOR DEMO
holder2 = externalLabData.loc[:, 'sampleID']
# Need to actually access the single cell value (string) in holder using .item()
# FOR DEMO
booleanHolder = holder2 == holder.item()
# FOR DEMO
holder3 = externalLabData.loc[booleanHolder, 'concentrationCO2']

# This is all of the above intermediate variables put together into one line, and is the right hand side of the
# assignment into the 'concentrationCO2Air' column of the outputDF data frame (at row 'x')
# FOR DEMO
rhsOfAssignment = externalLabData.loc[externalLabData.loc[:, 'sampleID'] == outputDF.loc[
outputDF.index[[x]], 'referenceAirSampleID'].item(), 'concentrationCO2']

# This needs to be in a try block because the outputDF has sampleIDs that are populated from the field samples,
# and this is looping through those sampleIDs to look for matches with the lab data, but for recently
# collected samples, there won't be lab data for them (but will obviously still be field samples) so this
# look-up will fail. Put it in a try block with nothing in the except block to imitate the silent try
# that this code has in R
try:
outputDF.loc[outputDF.index[[x]], 'concentrationCO2Air'] = externalLabData.loc[
externalLabData.loc[:, 'sampleID'] == outputDF.loc[
outputDF.index[[x]], 'referenceAirSampleID'].item(), 'concentrationCO2'].item()
# NEEDS THE OTHER ASSIGNMENT LINE HERE (THERE ARE 2 ASSIGNMENT LINES WITHIN EACH TRY BLOCK IN THE R CODE)

except Exception:
pass

0 comments on commit 734ff0e

Please sign in to comment.