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")

2 Answers 2

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")
4
  • 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, 2016 at 19:45
  • 2
    @geewest Negative, the layer references the underlying feature class, so updating the layer updates the feature class. Nov 23, 2016 at 0:00
  • @Hornbydd True. Edited. Nov 23, 2016 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, 2016 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 and acknowledge that you have read and understand our privacy policy and code of conduct.

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