Calculate Field using Python while retaining null values

744
1
04-22-2010 10:49 AM
KatieBudreski
New Contributor II
I am trying to use the Calculate Field tool in Model Builder using python (not VB) to convert from meters to miles.

Some records are null, in which case I want them to stay as null.

So, in the expression line (using Python 9.3), if I use the following, then nulls become 0, where in VB these would remain as null:

(!HabitatDistance!) * 0.000621371192

I've also tried to use Calc and Reclass with a variety of if, else statements, but none of those seem to work. I just get 0's for null input values.

e.g.

Expression: Calc(float(!HabitatDistance_mi!),float(!HabitatDistance_m!))

Codeblock:
def Calc(dist1,dist2):
    if dist2 == None:
        return None
    else:
        return dist2 * 0.000621371192


Any solutions?
0 Kudos
1 Reply
DarshaHardy
New Contributor II
I had a problem (in 9.2) with nulls and cursors in a python script. Basically, not possible to insert a null. I was getting the value from the row above being copied down instead - absolutely useless. Apparently a known bug, fixed for 9.4/10 and fixed with a service pack for 9.3 - no fix for 9.2 though.

Just found this thread http://forums.esri.com/Thread.asp?c=93&f=1729&t=262236

The comment "Just some advice some might find useful: The code block option in the Calculate Field tool is nothing more than a gp update cursor in disguise" is interesting. Maybe it's the same problem.
0 Kudos