Select to view content in your preferred language

Alter Field Name Using Python Issue

439
8
Jump to solution
03-24-2025 10:43 AM
ClintSteeves1
Occasional Contributor

I'm using Python3 to update some fields in a populated feature class with new names using AlterFields (multiple). This is an example of my code:

alterFieldsDescription = [
['FIELD_1', 'newName_1'],
['FIELD_2', 'newName_2'],
['FIELD_3', 'newName_3']
]

arcpy.management.AlterFields(featureClass, alterFieldsDescription)

I am getting this error code:
ERROR 001658: Cannot alter field types on populated tables.

As far as I can tell, I am not altering the field types, only the field names with my code. My formatting is consistent with the example on the ESRI tool reference website except I am not including the '#' sign in the non-applicable optional parameters. Do I absolutely need to include the '#' signs so the tool knows I am only wanting to change the Field Name? Or am I missing some other obvious thing? I'm altering a lot of field names, so even copying/pasting the '#' signs for each field will be tedious

 

Example with '#' signs: 

featureClass = 'pathToFeatureClass'
alterFieldsDescription = [
['FIELD_1', 'newName_1', '#', '#', '#', '#'],
['FIELD_2', 'newName_2', '#', '#', '#', '#'],
['FIELD_3', 'newName_3', '#', '#', '#', '#']
]

arcpy.management.AlterFields(featureClass, alterFieldsDescription)

 

I've checked that the path to my feature class is correct, my variable names are correct, all listed fields exist, and all field names are correctly capitalized. 

I'm using arcpy.management.AlterField(featureClass, 'FieldName', 'newFieldName') earlier in the script for a different populated feature class, and that works exactly as intended. Is there something different with using AlterFields (multiple)?

I'd like to avoid adding new fields, updating the values of the new fields from the old fields, and deleting the old fields. I'd also like to avoid altering each field individually.

Thank you

0 Kudos
1 Solution

Accepted Solutions
ClintSteeves1
Occasional Contributor

I think I found the cause of the issue by using the Alter Fields tool right in Pro. It seems like it defaults to changing the field type to Long regardless of what the field type actually is. Seems pretty ridiculous that it isn't reading the field type and keeping it the same unless otherwise specified. I'll be reporting it as a bug, but who knows if it'll ever get fixed. So the fix in my code is to include the field type and length (where necessary). 

I set up the Alter Fields tool with all the fields I'd like to rename, and made sure they were all the right field type by cross-referencing with the fields view. Ran the tool to make sure it worked, then copied and pasted the command into my python script.

This is how the script directly from the Alter Fields tool pasted:

arcpy.management.AlterFields(
    in_table=featureClass,
    field_description="FIELD1 newName1 # TEXT 255 # #;FIELD2 newName2 # LONG # # #"
)

The formatting isn't consistent with what's on the tool reference website, but it works. I've updated the formatting to be consistent with what's on the tool reference website because it reads much easier, and that is now also working. 

Just seems crazy to me that the Alter Fields tool wants to update the field type no matter what, and that it doesn't want to default to what the field type is. Definitely feels like an oversight on ESRI's part. I'm going to mark this as solved, but will try to submit it as a bug. 

View solution in original post

8 Replies
AustinAverill
Frequent Contributor

I've never used this tool to experience the issue so can't speak to the use of "#". 

 

Worst case scenario you can just use AlterField on a loop of a dictionary of the fields you want to alter. I know you mentioned that you aren't necesarrily wanting to run the AlterField function individually for ecah field. But if this is a one off script or something to update some values, it will get you by. Something like: 

flds = {
    'field1' : 'field1_new',
    'field2' : 'field2_new'
}

for key in flds.keys():
    arcpy.management.AlterField(key, flds[key]) 

 

0 Kudos
ClintSteeves1
Occasional Contributor

Thanks for the suggestion. I just tried to use '#' in the exact formatting on the ESRI site, and am now just getting a general error message without an error code. 

I'm not terribly familiar with using dictionaries, but would I need to replace 'key' in AlterField with my feature class name? 

flds = {
   'field1' : 'field1_new',
   'field2' : 'field2_new'
}

for key in flds.keys():
   arcpy.management.AlterField(featureClass, flds[key])

 

0 Kudos
AustinAverill
Frequent Contributor

With dictionaries, when you loop through keys like a provided there, `key` would return the first field name, and then `flds[key]` would return the new field name. So with the following code - 

flds = {
    'field1' : 'field1_new',
    'field2' : 'field2_new'
}

for key in flds.keys():
    print(f"Old Name: {key} | New Name: {flds[key]}")

 

you would expect to get

Old Name: field1 | New Name: field1_new
Old Name: field2 | New Name: field2_new

 

So in this, for your use case you would just make sure that you dictionary is written such that each pair is a old name and a new name.

0 Kudos
DanPatterson
MVP Esteemed Contributor

What version of Arcgis Pro? (hence arcpy)

Do you have integer fields?  

Was the table old? using short integer vs long integer?

Note in the snippet that the entry for the parameters is a space delimited string.

I just did a test and copies the tool parameters and got this for one field

Alter Fields (multiple)
=====================
Input Table     maple_leaf_multi
Field Properties     ID_arr ID_orig # LONG # # #
Updated Input Table     maple_leaf_multi
=====================

The LONG was prompted for confirmation by the tool.

 

copied python snippet

arcpy.management.AlterFields(
    in_table="maple_leaf_multi",
    field_description="ID_orig ID_orig # LONG # # #"
)

I would check check a manual run of the tool again


... sort of retired...
0 Kudos
ClintSteeves1
Occasional Contributor

Thanks Dan. I didn't refresh the page before putting in my own reply, so I didn't see your response until afterwards. I saw very similar results to what you put in your response. 

0 Kudos
DanPatterson
MVP Esteemed Contributor

Glad you mirrored my response and found a solution in any event.


... sort of retired...
ClintSteeves1
Occasional Contributor

I think I found the cause of the issue by using the Alter Fields tool right in Pro. It seems like it defaults to changing the field type to Long regardless of what the field type actually is. Seems pretty ridiculous that it isn't reading the field type and keeping it the same unless otherwise specified. I'll be reporting it as a bug, but who knows if it'll ever get fixed. So the fix in my code is to include the field type and length (where necessary). 

I set up the Alter Fields tool with all the fields I'd like to rename, and made sure they were all the right field type by cross-referencing with the fields view. Ran the tool to make sure it worked, then copied and pasted the command into my python script.

This is how the script directly from the Alter Fields tool pasted:

arcpy.management.AlterFields(
    in_table=featureClass,
    field_description="FIELD1 newName1 # TEXT 255 # #;FIELD2 newName2 # LONG # # #"
)

The formatting isn't consistent with what's on the tool reference website, but it works. I've updated the formatting to be consistent with what's on the tool reference website because it reads much easier, and that is now also working. 

Just seems crazy to me that the Alter Fields tool wants to update the field type no matter what, and that it doesn't want to default to what the field type is. Definitely feels like an oversight on ESRI's part. I'm going to mark this as solved, but will try to submit it as a bug. 

DavidSolari
MVP Regular Contributor

If the issue is just copy-pasting those last few empty items:

alterFieldsDescription = [
['FIELD_1', 'newName_1'],
['FIELD_2', 'newName_2'],
['FIELD_3', 'newName_3']
]

alterFinal = [x + ["#", "#", "#"] for x in alterFieldsDescription]

arcpy.management.AlterFields(featureClass, alterFinal)