AnsweredAssumed Answered

Python Calculatefield error: incompatible with the field type

Question asked by afrocodes on Mar 10, 2015
Latest reply on Mar 10, 2015 by afrocodes

Hi all,


I have been having trouble with getting Calculatefield_management to accept my SQL expression variables. I have tried various ways to enter the variables to get the first two calculate field functions to work.


The error is now occurring with the 3rd calculate field/expression3/calculating the NA in the code below. [arcpy.CalculateField_management(inFeatures, fieldName3, expression3, "PYTHON_9.3")]


I get different errors depending on how I enter in the variable.


One of the errors I get while debugging now:

Traceback (most recent call last):

  File "", line 175, in <module>

    arcpy.CalculateField_management(inFeatures, fieldName3, expression3, "PYTHON_9.3")

  File "C:\Program Files (x86)\ArcGIS\Desktop10.2\arcpy\arcpy\", line 3354, in CalculateField

    raise e

ExecuteError: ERROR 999999: Error executing function.

The value type is incompatible with the field type. [NA]

Failed to execute (CalculateField).



The first calculatefield updates the RG field based on the numbers in the !Join_Count! features column. RG is a short integer.

The second calculatefield updates the PG field which is (!RG!/sum of RG) * 100. PG should be a float since it's finding the percentage.

The third calcualtefield udpates the NA field which is !PG! * 12425. NA should be a short integer.


Any suggestions on how I can write expression3 to be the correct field type format for NA? Thanks.


My Background

-Novice Programmer

-Using the Python 2.75 Idle Shell

-Using ArcMap 10.2.1


Sample Code Snippet:

# Import ArcPy site-package to use Arcgis and os modules to use operating system
import arcpy, os, sys, string, numpy
from arcpy import env
env.overwriteOutput = True

#set environment settings to where mxd is saved
env.workspace = r'J:\folder\scripts'

output = r'J:\folder\scripts\output' + "\\"

inFeatures = output + "joined_" + + "_" + + ".dbf"
print inFeatures

#First Calculate Field from spatial join count numbers 
fieldName = "RG"    #SHORT
expression = '!Join_Count!'   #LONG type
arcpy.CalculateField_management(inFeatures, fieldName, expression, "PYTHON_9.3")

print "Join Count numbers added to RG Field"

#Need sum of rg...
arr = arcpy.da.FeatureClassToNumPyArray(inFeatures, ('RG'))

# Sum the RG field
print "The total sum of features in RG is: "

rg_sum = arr["RG"].sum()
print rg_sum 
flnum = "!RG!"
print flnum

#Calculate PG which equals (!RG!/Sum of !RG!) * 100
fieldName2 = "PG"   #FLOAT type, it's a percentage number
arcpy.CalculateField_management(inFeatures, fieldName2, "(float('!RG!')) / " +  str(rg_sum) + "* 100" , "PYTHON_9.3")

arr = arcpy.da.FeatureClassToNumPyArray(inFeatures, ('PG'))
PG_sum = arr["PG"].sum()
print "The total sum of features in PG is: " + str(PG_sum)  #should equal 100.0

# Calculate NA, which equals !PG! * 12425
fieldName3 = "NA"   #SHORT type, it needs to be a whole number
expression3 = "'!PG!' * 12425"
arcpy.CalculateField_management(inFeatures, fieldName3, expression3, "PYTHON_9.3")

arr = arcpy.da.FeatureClassToNumPyArray(inFeatures, ('NA'))
NA_sum = arr["NA"].sum()
print "The total sum of features in NA is: " + str(NA_sum)  #needs to equal 12425