Select to view content in your preferred language

Nothing Happens When I run arcpy.management.AlterField()

878
5
Jump to solution
04-03-2023 05:40 PM
DarrenConly
Occasional Contributor

I'm trying to write a function that takes in a feature class and converts all its field names to uppercase, using the Alter Field tool. Here is my code:

def capitalize_fc_fnames(in_fc):
    # capitalizes all field names for specified feature class
    print("\nCapitalizing all field names for master network output feature class..")
    all_fields = [f.name for f in arcpy.ListFields(in_fc)]
    for f in all_fields:
        arcpy.management.AlterField(in_fc, f, new_field_name=f.upper())

 

I am running the function on a feature class in a file geodatabase. The function runs without errors, but when I open the feature class, none of the fields have been altered.

I have also tested in Pro using the Alter Field GUI tool on just one field, but with that nothing happens: no errors, no changes to the field names.

This post from 2017 suggests that at that time it was a known but unresolved bug that Alter Field did nothing. I *really* hope that's not still the case as I have 90+ fields I want to convert to uppercase. @BrandonFlessner did you ever find a solution to this? @DanPatterson do you have any sage input on this?

Thanks,

Tags (3)
0 Kudos
1 Solution

Accepted Solutions
by Anonymous User
Not applicable

I wonder if this is the extension of how it also has problems when trying to do it in catalog- it wont change cases if the names are the same. Just reverts back to whatever case it was... I'd try appending a temp value and then change it back to the desired name:

def capitalize_fc_fnames(in_fc):
    # capitalizes all field names for specified feature class
    print("\nCapitalizing all field names for master network output feature class..")
    all_fields = [f.name for f in arcpy.ListFields(in_fc)]
    for f in all_fields:
        arcpy.management.AlterField(in_fc, f, new_field_name=f'{f.upper()}_cmon')
        arcpy.management.AlterField(in_fc, f'{f.upper()}_cmon', new_field_name=f.upper())

 

View solution in original post

5 Replies
DanPatterson
MVP Esteemed Contributor

syntax looks fine, but I would exclude fields that are not editable or required in your list comprehension

Field—ArcGIS Pro | Documentation

The only things that bug me right now are:

  •  is the full path to the featureclass passed to the script?
  • where are you running the function from (separate IDE, python IDE, notebook??)
  • when you open the featureclass.... it won't update unless run from a script tool in arctoolbox or the python window.
  • and.... is this a network dataset? (I think they have other constraints compared to "regular" featureclasses)

 


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

Hi Dan,

  •  is the full path to the featureclass passed to the script? Yes
  • where are you running the function from (separate IDE, python IDE, notebook??) In a separate IDE in a python script, but I tested in Pro's python command prompt as well. Neither worked.
  • when you open the featureclass.... it won't update unless run from a script tool in arctoolbox or the python window. I was running from the python command prompt in pro
  • and.... is this a network dataset? (I think they have other constraints compared to "regular" featureclasses) No, just a line feature data set

 

@Anonymous User's answer seems to work (i.e., change to a different name, then change back to the capitalized version of the original name). But that's still not ideal because it moves the field name to the end.

0 Kudos
by Anonymous User
Not applicable

I wonder if this is the extension of how it also has problems when trying to do it in catalog- it wont change cases if the names are the same. Just reverts back to whatever case it was... I'd try appending a temp value and then change it back to the desired name:

def capitalize_fc_fnames(in_fc):
    # capitalizes all field names for specified feature class
    print("\nCapitalizing all field names for master network output feature class..")
    all_fields = [f.name for f in arcpy.ListFields(in_fc)]
    for f in all_fields:
        arcpy.management.AlterField(in_fc, f, new_field_name=f'{f.upper()}_cmon')
        arcpy.management.AlterField(in_fc, f'{f.upper()}_cmon', new_field_name=f.upper())

 

DarrenConly
Occasional Contributor

Hi Jeff,

This seems to work after testing out in the Pro python window. This approach also seems to move the field to the end, which is not ideal. I might end up using it if nothing else comes to mind.

0 Kudos
by Anonymous User
Not applicable

This might be a view/cache thing, or under the hood (more likely) it is creating a new field with the new name, and then doing a migrate from the old field so that would put them at the end.  If you could, can you create a new FC and field map the data to the uppercased fields? then save over the old?

0 Kudos