Select to view content in your preferred language

arcpy.UpdateCursor setting a row value to null instead of 0

5332
2
09-23-2015 03:42 PM
ShaneArmour
Deactivated User

Hi,

I'm having issues setting a row value to 0 when joining a csv to a shapefile. When the shapefile value is not in the csv, the row is getting set to 0 instead of null. This is a huge problem as i'm showing 0 change instead of no data. I tried row.setValue(field, None) and row.setNull(field) and both produce the same result. In the .dbf file of the shapefile, there is no value but in the UI attribute table a 0 is shown. Any advice on how to fix this would be very appreciated.

Thanks!

Shane

    arcpy.AddField_management(shpfile, "TEST", "DOUBLE", field_is_nullable = 'NULLABLE')

    newcols = ["TEST"]

    with open(csvfile, 'rb') as csvfile:

        lib = dict()

        csvfile = csv.reader(csvfile, delimiter = ",")

        csvfile.next() #skip the headers

        for line in csvfile:

            lib[line[csvjoinindex]] = lib.get(line[csvjoinindex],line[csvstartfield:])

    rows = arcpy.UpdateCursor(shpfile)

    for row in rows:

        shpjoinval = str(row.getValue(shapefilejoincol))

        try:

            vals = lib.get(shpjoinval)

            for ind, field in enumerate(newcols):

                row.setValue(str(field),vals[ind])

                rows.updateRow(row)

        except:

            for ind, field in enumerate(newcols):

                 row.setNull(field)

                 rows.updateRow(row)

Tags (2)
0 Kudos
2 Replies
FreddieGibson
Honored Contributor

You'd need to use a geodatabase feature class instead of a shapefile to accomplish this. Shapefiles do not support NULL values. This is documented on the following page:

Add Field (Data Management)

http://desktop.arcgis.com/en/desktop/latest/tools/data-management-toolbox/add-field.htm

As shown in the screenshot below, Null values are only supported for fields in a geodatabase.

2015-09-23_1606.png

DanPatterson_Retired
MVP Emeritus

specifics to shapefiles regarding nulls or lack thereof

Data type containing null valueNull value substitution

Number—When tool requires that a NULL, infinity, or NaN (Not a Number) be output

-1.7976931348623158e+308 (IEEE standard for the maximum negative value)

Number (all other geoprocessing tools)

0

Text

" " (blank—no space)

Date

Stored as zero, but displays <null>

from here  Geoprocessing considerations for shapefile output—Help | ArcGIS for Desktop