1

I have a script that runs every seven minutes and would like to calculate only rows that are Null.

How should this be done?

This is my initial script which calculates all fields each time.

    import arcpy
    calcExpression =  "!ORIGDTDATE!+\" \"+ !ORIGDTTIME!"
    arcpy.CalculateField_management(fc,"emergency_overdue",calcExpression,"PYTHON_9.3")
    arcpy.CalculateField_management(fc,"emergency_overdue",calcExpression,"PYTHON_9.3")
3

Create a feature layer with an SQL statement that selects only NULL values. Let's say the field in question is SOMEFIELD:

import arcpy

fld = "SOMEFIELD"

#create SQL
delimFld = arcpy.AddFieldDelimiters (fc, fld)
sql = "{} IS NULL".format (delimFld)

#create feature layer
arcpy.MakeFeatureLayer_management (fc, "layer", sql)

#calc
calcExpression =  "!ORIGDTDATE!+\" \"+ !ORIGDTTIME!"
arcpy.CalculateField_management("layer", "emergency_overdue", calcExpression, "PYTHON_9.3")
  • That is so time consuming and inefficient, I should be able to use a conditional statement in the field calculator. I would then have to append my layer back to the feature class. – geewest Nov 22 '16 at 19:45
  • 2
    @geewest Negative, the layer references the underlying feature class, so updating the layer updates the feature class. – Emil Brundage Nov 23 '16 at 0:00
  • @Hornbydd True. Edited. – Emil Brundage Nov 23 '16 at 15:46
  • 1
    Generally speaking, it's faster to use multiple Make Feature Layers then build multiple if..elif statements inside Calculate Field. Create a table with a few million records and you'll see large differences. – Paul Nov 23 '16 at 18:00
2

You need to create a layer or table view of your data as they are objects that understand the concept of a selection. Then you select the rows with the select by attribute tool where your field is Null then run your field calculations. As these tools honour selections only the rows that are selected will be updated.

To see an example of this as code please refer to @Emil Brundage answer above.

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.