Python Calculatefield error: incompatible with the field type

5644
5
Jump to solution
03-10-2015 07:27 AM
LisCollins
Occasional Contributor

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 "test.py", line 175, in <module>

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

  File "C:\Program Files (x86)\ArcGIS\Desktop10.2\arcpy\arcpy\management.py", 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).

Method

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'

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

inFeatures = output + "joined_" + c.name + "_" + w.name + ".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
0 Kudos
1 Solution

Accepted Solutions
LisCollins
Occasional Contributor

I changed NA to long and put int inside the variable like below and it now works!

Thanks for your suggestion for changing it to long!

expression3 = "int(float('!PG!') * 12425)"

View solution in original post

0 Kudos
5 Replies
LisCollins
Occasional Contributor

And if I try the the code below to make PG float and make the whole result an integer, I get this error:

Traceback (most recent call last):

  File "test.py", line 174, in <module>

    expression3 = int("float('!PG!') * 12425")

ValueError: invalid literal for int() with base 10: "float('!PG!') * 12425"

expression3 = int("float('!PCTGROWTH!') * 12425")
arcpy.CalculateField_management(inFeatures, fieldName3, expression3, "PYTHON_9.3")
0 Kudos
MahtabAlam1
Occasional Contributor

It seems your value is crossing the limit that short int can handle which is -32,768 to 32,767.   You should change it to long.

HTH

LisCollins
Occasional Contributor

@

0 Kudos
MahtabAlam1
Occasional Contributor

Please remove the single quotes within your expression around field name. Change your expression to something like this:

"!PG!*12425"

0 Kudos
LisCollins
Occasional Contributor

I changed NA to long and put int inside the variable like below and it now works!

Thanks for your suggestion for changing it to long!

expression3 = "int(float('!PG!') * 12425)"

0 Kudos