Select to view content in your preferred language

Field Calculator with Python Code Block runs but returns null

1396
4
Jump to solution
11-11-2019 01:41 PM
NealBanerjee
Frequent Contributor

Hello,

I have a Python expression that utilizes a code block that I am trying to run as a field calculation (manual or with Field Calculator geoprocessing tool).  The code runs without issue, but returns nulls even if I hard-code the return value to a simple string.  I have tried going through line by line to better understand where/why it is not working.  Best I can tell is it fails once I try to round a number.  The code does include some lists, dictionaries, and loops, but it works fine when I run in an independent Python IDE with hardcoded input values.

In the code snippet below, Ive hard coded the function just to return a "TEST" (see near bottom), but it return null.  If I take out the line with round statement it does at least return "TEST". I think Ive checked all my indentation, but cant think of anything else that may be causing the issue

Does field calculator have any limitations with lists, loops, etc.?  Any ideas what may be causing this?

Any thoughts would be greatly appreciated!

Thanks

Neal

-------------------- Code Snippet ---------

def Calc(ScrA, ScrB, ScrC, ScrD, ScrE, ScrF, ScrG, ScrH, ScrI, ScrJ, ScrK, ScrL, ScrM, multN, multO, multP, multQ, ScrTot):
  dicComMult={multN:"N", multO:"O", multP:"P",multQ:"Q"}
  maxMult = max(dicComMult)
  if maxMult > 1:
    maxMultCom = dicComMult[max(dicComMult)]
    ScrMult = round(ScrTot - (ScrTot / maxMult),1)
  else:
    maxMultCom='NA'
    ScrMult = 0

  lstCom = [("A", ScrA), ("B", ScrB), ("C", ScrC), ("D", ScrD), ("E", ScrE), ("F", ScrF), ("G", ScrG), ("H",ScrH), ("I", ScrI), ("J", ScrJ), ("K", ScrK), ("L", ScrL), ("M", ScrM), (maxMultCom,ScrMult)]
  lstHCFs=[]
  cumPctScr=0

  for Com in lstCom:
    val=round(Com[1],1)
    strlist=str(val)
  return "TEST"

------------------------------------------------

Example Field Calculator that returns null

0 Kudos
1 Solution

Accepted Solutions
NealBanerjee
Frequent Contributor

I just realized my problem.  Apparently some of the records in the table that I was calculating had null values.  This seems to be throwing off numerical functions.  I replace null with 0 values in the code and it is populating the field.  Thank you Dan and Joshua for taking a look

View solution in original post

4 Replies
DanPatterson_Retired
MVP Emeritus

Neal can you format it so alignment can be checked

/blogs/dan_patterson/2016/08/14/script-formatting 

not sure what that round section is doing and whether it is aligned

JoshuaBixby
MVP Esteemed Contributor

I agree, the indentation can make a big difference, and in this case could easily explain why "TEST" isn't being returned.

0 Kudos
NealBanerjee
Frequent Contributor
def Calc(ScrA, ScrB, ScrC, ScrD, ScrE, ScrF, ScrG, ScrH, ScrI, ScrJ, ScrK, ScrL, ScrM, multN, multO, multP, multQ, ScrTot):
  dicComMult={multN:"N", multO:"O", multP:"P",multQ:"Q"}
  maxMult = max(dicComMult)
  if maxMult > 1:
    maxMultCom = dicComMult[max(dicComMult)]
    ScrMult = round(ScrTot - (ScrTot / maxMult),1)
  else:
    maxMultCom='NA'
    ScrMult = 0

  lstCom = [("A", ScrA), ("B", ScrB), ("C", ScrC), ("D", ScrD), ("E", ScrE), ("F", ScrF), ("G", ScrG), ("H",ScrH), ("I", ScrI), ("J", ScrJ), ("K", ScrK), ("L", ScrL), ("M", ScrM), (maxMultCom,ScrMult)]
  lstHCFs=[]
  cumPctScr=0
  for Com in lstCom:
     PctScr = round(Com[1] / ScrTot * 100,1)

  strlist=str(PctScr)
  return "TEST"

The code above returns "null" (not expected) when I run it

The code below returns "TEST" (as expected) if I simply remove the round and division on line 15.  I did not change any indentation in the field calculator

def Calc(ScrA, ScrB, ScrC, ScrD, ScrE, ScrF, ScrG, ScrH, ScrI, ScrJ, ScrK, ScrL, ScrM, multN, multO, multP, multQ, ScrTot):
  dicComMult={multN:"N", multO:"O", multP:"P",multQ:"Q"}
  maxMult = max(dicComMult)
  if maxMult > 1:
    maxMultCom = dicComMult[max(dicComMult)]
    ScrMult = round(ScrTot - (ScrTot / maxMult),1)
  else:
    maxMultCom='NA'
    ScrMult = 0

  lstCom = [("A", ScrA), ("B", ScrB), ("C", ScrC), ("D", ScrD), ("E", ScrE), ("F", ScrF), ("G", ScrG), ("H",ScrH), ("I", ScrI), ("J", ScrJ), ("K", ScrK), ("L", ScrL), ("M", ScrM), (maxMultCom,ScrMult)]
  lstHCFs=[]
  cumPctScr=0
  for Com in lstCom:
     PctScr = Com[1]

  strlist=str(PctScr)
  return "TEST"
0 Kudos
NealBanerjee
Frequent Contributor

I just realized my problem.  Apparently some of the records in the table that I was calculating had null values.  This seems to be throwing off numerical functions.  I replace null with 0 values in the code and it is populating the field.  Thank you Dan and Joshua for taking a look