When using Python in a Model Builder Calculate Field operation and one of the input field values is null, code does not execute

881
5
Jump to solution
10-22-2020 09:42 AM
MikeSayre
New Contributor II

I'm trying to convert some VB Calculate Field code to Python for use in ArcGIS Pro.  This code would work in VB, but I'm unable to get the Python syntax to execute in all scenarios.  I have a simplified example of what I trying to do below:

Field name = type

DetermineType(!lanes!,!speed!)  - this goes in the expression box

The following is in the code block:

def DetermineType(Lanes, Speed):

   if Lanes.rstrip() == "1" and int(Speed.rstrip()) < 30:

      return 1

   elif Lanes.rstrip() == "2" and int(Speed.rstrip()) < 30:

      return 2

   elif Lanes.rstrip() == "3" and int(Speed.rstrip()) > 30:

      return 3

   else:

      return 4 

This will work as long as a row of data has values for both the Lanes and Speed field attributes.  However, if one of these fields is null, I would expect a value of 4 to be returned to populate the type field but instead, my code block doesn't get executed at all.  I end up with null in the type field.  

My question is this:  How do I populate a field if either of the input fields used to determine its value is null?

By the way, I've tried several experiments, including just concatenating !lanes! and !speed! in the Expression box, but as long as at least one of those fields is null, I get a null result.

0 Kudos
1 Solution

Accepted Solutions
MikeSayre
New Contributor II

To follow up, I was able to get this working by bringing my original model into ArcGIS Pro, editing the syntax of the calculate field functions there, and including tests for None at the beginning of the functions.  I had previously been editing the Python code in ArcMap, and when I exported the functionality to a python script and tried to run it using ArcPro libraries, it wasn't the exact flavor of Python needed by Pro.  

View solution in original post

0 Kudos
5 Replies
JoeBorgione
MVP Emeritus

Here is what your code looks like when you use the syntax highlighter:

def DetermineType(Lanes, Speed):

   if Lanes.rstrip() == "1" and int(Speed.rstrip()) < 30:

      return 1

   elif Lanes.rstrip() == "2" and int(Speed.rstrip()) < 30:

      return 2

   elif Lanes.rstrip() == "3" and int(Speed.rstrip()) > 30:

      return 3

   else:

      return 4 

You need to check for the <Null> values specifically with:

...

elif Lanes.rstrip() is None:
   do what you need to do...
That should just about do it....
0 Kudos
MikeSayre
New Contributor II

Thanks, but that's not working because the program execution never gets into the "if" statement at all if Lanes or Speed are null/None.  If you try to do a function call by passing in a null value, it will bypass the function completely and move onto the next step in the Model.

0 Kudos
JoeBorgione
MVP Emeritus

Guess I missed that it's a function.  How are you calling the function?  Can you trap for <Null> values before hand, and then pass something like -999 to your function?

That should just about do it....
0 Kudos
MikeSayre
New Contributor II

A colleague of mine find an article that might help me:  How To: Calculate fields where some inputs are null values 

It appears at this point that if I do the test for None at the beginning of the function, instead of down in the existing if statement, it may work.  I'm currently testing this change.

0 Kudos
MikeSayre
New Contributor II

To follow up, I was able to get this working by bringing my original model into ArcGIS Pro, editing the syntax of the calculate field functions there, and including tests for None at the beginning of the functions.  I had previously been editing the Python code in ArcMap, and when I exported the functionality to a python script and tried to run it using ArcPro libraries, it wasn't the exact flavor of Python needed by Pro.  

0 Kudos