Add field returns type long not type short

179
2
01-06-2022 12:22 PM
GerryGabrisch
Occasional Contributor III

I have a ArcGIS Pro v2.8 stand-alone Python script.  This script create a new shapefile via the mean center tool.  I use python to add a new field to this mean center output as a type short.  After viewing the resulting shapefile in ArcGIS Pro I see that the attribute table is showing my TagID attribute but ArcGIS Pro is referring to the attribute as a long!

I can confirm that QGIS 3.10 shows the TagID as an integer.  I can confirm that I can populate the attribute table with a numeric value in both QGIS and ArcGIS Pro manually.

After I try to populate the new attribute with a calculate field I get the following error.

ERROR: ArcPy ERRORS:
ERROR 160176: The index passed was not within the valid range.
Failed to execute (CalculateField).

 Here is a link to the tool error...160176 error 

Here is the relevant code:

 

 

 

 

arcpy.AddField_management(out_fc, 'TagID', "SHORT")

tagid = '"TagID"'
value = str(int(tag))
arcpy.CalculateField_management(out_fc, tagid, value,  "PYTHON3")

 

 

 

Thinking that an AddIndex might save the problem I execute the mean center tool then do an Add Index.  This gets me to the CalculateField but passing that crashes arcpy.

 

Why is ArcGIS Pro showing the attribute type as Long?  Why am I getting this error?  How can I fix the problem?

0 Kudos
2 Replies
GintautasKmieliauskas
Regular Contributor II

Hi,

CalculateField_management has optional parameter for Field Type specifying. Try to specify Short because python by default has only int. So its too big for your field and needs to convert it.

0 Kudos
JeffK
by MVP Regular Contributor
MVP Regular Contributor

You've got a few things going on here...

What is tag?

I don't think you need to double quote TagID. 

If your value is supposed to be a short, why are you converting it to a string in the expression?

arcpy.AddField_management(out_fc, 'TagID', "SHORT")

tagid = '"TagID"'
value = str(int(tag))
arcpy.CalculateField_management(out_fc, tagid, value,  "PYTHON3")

try something like:

arcpy.CalculateField_management(out_fc, 'TagID', int(tag),  "PYTHON3")

 

0 Kudos