2

I'm using the following python code to replace Null values by 0, using field calculator:

def nulls(p1):
 if p1 is None:
    return 0
 else:
    return p1

But nothing happens, Null values stay. I'm using ArcGIS Desktop 10.0.

  • I only have 10.3 but I copy and pasted your code and it worked for me. Is there an error? Does it work correctly maintain non null values? (Test by running this code bu to a new/different field so we can check if nothing is happening or just the null part isn't working. Any errors? – DMusketeer Oct 7 '15 at 10:34
  • 1
  • There's no error, Null values stay, doesn't turn to 0. It works in other fields, I think the problem is in the null part, because in other fields I don't have nulls and it returns "p1" – AndM Oct 7 '15 at 10:48
  • ESRI Bug. Does my answer provide a usable work around? – DMusketeer Oct 7 '15 at 10:57
  • @AndM Given the complexity that this now presents in order to implement in model builder. It may be easier going back a step. Why do you have null values? Can they be intercepted before this stage? Why are you changing null to 0? – DMusketeer Oct 7 '15 at 14:30
1

Since your code worked for me, a work around would be to use 'select by attribute' to select null rows e.g. MyField IS NULL. Once you have them all selected just use field calculator e.g. MyField = 0.

3

I recently had to replace Null values as well, but instead of the field calculator I used a short python script including an UpdateCursor, maybe this helps you:

with arcpy.da.UpdateCursor("feature_class, ["field_with_null_values"]) as cursor:
    for row in cursor:
        if row[0] == None:
            row[0] = 0
            cursor.updateRow(row)

print "Processing complete"

del row

the only thing I don't know is if this works for 10.0, I work with 10.2

At 10.0, which does not have the Data Access module (arcpy.da), the code using old style cursors would look more like this:

cursor = arcpy.UpdateCursor(inFeatureClass)
for row in cursor:
    if row.getValue(inField) == None:
        row.setValue(inField,0)
    cursor.updateRow(row)
del row,cursor
  • I'm using field calculator in modelbuilder, and I don't know how to use your code in modelbuilder. How can I introduce a python code in a model? – AndM Oct 7 '15 at 13:12
  • @AndM You will need to implement the code in this answer into a script tool to use in model builder. Once you have it in an ArcMap toolbox, you can incorporate it into model builder. If you don't have experience of this, you will need to do a few searches and maybe ask some separate questions along the way if you get stuck. The process is much beyond the scope of this question. – DMusketeer Oct 7 '15 at 14:27
  • @danielaandrea thanks for your help, i'm going to see if it worths to do that. – AndM Oct 7 '15 at 15:36
  • I added code that should work at 10.0 (before arcpy.da existed). It's what I used at that time. – PolyGeo Oct 8 '15 at 5:17
1

The underlying cause of this problem seems to be bug number NIM059424 which was present in ArcGIS Desktop 10.0 and resolved by ArcGIS 10.1 for Desktop:

Null values in the feature class do not get replaced when using Python in the Field Calculator.

0

Your original code will work only if you are on edit session. Start editing the feature and run the code again. I´m having the same problem to calculate on Model builder.I´m using ArcGIS 10.4.

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.