How do you calculate fields with null values? I have multiple fields that may have null values, that aren't being calculated in the third field.

enter image description here

import arcpy
with arcpy.da.UpdateCursor(r"...gdb\test.gdb\test",["A", "B", "C"]) as cursor:
    for row in cursor:
        row[2] = row[0] + row[1]

The result of that calculation is this error:

Traceback (most recent call last):
  File "C:\Users\Desktop\updatecursor2.py", line 4, in <module>
    row[2] = row[0] + row[1]
TypeError: unsupported operand type(s) for +: 'NoneType' and 'float'

The result of the script are the same results when I try to use the field calculator:

enter image description here

How do you use the data update cursor, or field calculator to calculate the sum of the values in a new field if you have null values?

I'm using ArcGIS 10.1 SP1 for Desktop.

  • 1
    What are the data types of these fields? It looks like you are trying to set the value of field "A" (2) with the sum of the values from ObjectID (0), and Shape (1). I also don't think you are setting the values correctly. – jbchurchill Oct 1 '14 at 13:37
  • @jbchurchill The data type is Double. – user3767931 Oct 1 '14 at 13:38
  • 1
    @jbchurchill FYI, when using data access cursors, the index numbers correspond to the user-supplied attribute list (in this case ["A","B","C"]) and not the list of all fields in the table, so they were being referenced correctly. – nmpeterson Oct 1 '14 at 14:31
  • Ah I see that now. So those values are being set correctly after all. – jbchurchill Oct 1 '14 at 14:34
  • What do you want to do if A and/or B is Null? Give C the value of the non-Null field, if there is one, or skip it, leaving C Null? – recurvata Oct 1 '14 at 15:05

Here's a way to replace all the nulls with zeroes:

import arcpy
with arcpy.da.UpdateCursor(r"...gdb\test.gdb\test",["A", "B", "C"]) as cursor:
    for row in cursor:
        a_value = row[0] if row[0] else 0  # Use 0 when "A" is falsy
        b_value = row[1] if row[1] else 0  # Use 0 when "B" is falsy
        row[2] = a_value + b_value

Note: this doesn't actually update the null values in the table; it merely replaces them with zeroes during calculations. If you want to replace the nulls with zeroes permanently, you could change the penultimate line to row = [a_value, b_value, a_value + b_value].

  • 2
    You could take advantage of the way Python does boolean expressions and simply do a_value = row[0] or 0 – Jason Scheirer Oct 3 '14 at 22:00

Lets start with the field calculator. it should look like these 2 screenshots. Screenshot 1Screenshot 2 (result)

When I calculate fields inside of an Update cursor (update_cursor) it looks more like this. Here I have a SearchCursor (row is set to searchCursor.next) and an update Cursor (row2 is set to update_cursor.next). This is actually slightly different since I am using values in a different layer for the updates and a selection.

while row:
     while row2:
          grpnumValue = row2.getValue("A")
          if (grpnumValue > 0):
                row.setValue("MAILING_GROUP", grpnumValue)
          row2 = searchCursor.next
      row = update_cursor.next()    

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.