9

What I need to do:

  1. rename a field name of a table/feature class
  2. copy all values to the new field

So far I have done following code as @artwork21 suggests:

import sys
import traceback
import arcpy
from arcpy import env

## ARGUMENTS
# argv[1] = input table/feature class path
# argv[2] = input old field name
# argv[3] = input new field name  

path = sys.argv[1]
oldFieldName = sys.argv[2] 
newFieldName = sys.argv[3] 

env.overwriteOutput = True

fields = arcpy.ListFields(path)
for field in fields:
    if field.aliasName == oldFieldName:
        if not oldFieldName == newFieldName:
            fieldType = field.type
            # Add new field
            arcpy.AddField_management(path, newFieldName, fieldType)
            #Calculates the new field based on old field values
            arcpy.CalculateField_management(path, newFieldName, "!"+oldFieldName+"!", "PYTHON", "")
            # Delete the old field (if necessary)
            arcpy.DeleteField_management(path, oldFieldName)

How can I map field.type to AddField_management method's field type? And while the field is in a middle place, then field deleted from middle and added to the last. That does not looks like as if field name is renamed.

Is there any better solution that help me to do these things?

14
  • Your feature layer field is not being renamed here, fieldInfo.setNewName(index, "stat"). This is why you are getting your error on line row.setValue("stat", gValue).
    – artwork21
    Commented Feb 20, 2013 at 13:14
  • 2
    setNewName (index, new_field_name), must be for only setting new field names, not renaming existing ones.
    – artwork21
    Commented Feb 20, 2013 at 13:33
  • 2
    A FieldInfo object does not do anything to the underlying data, it is simply a representation of the field properties that can be used as an input to certain tools for creating new data, similar to a FieldMappings object.
    – blah238
    Commented Feb 20, 2013 at 20:14
  • 1
    Is old_field a variable name or the actual name of the old field? If it's a variable name you need to use string formatting or concatenation to wrap the value of the variable with the brackets (VB parser) or exclamation points (Python parser).
    – blah238
    Commented Feb 21, 2013 at 1:46
  • 1
    An alias in general is a "nickname" or a pseudonym. It is not necessarily equal to the actual column name in the database.
    – blah238
    Commented Feb 22, 2013 at 16:05

5 Answers 5

16

Try this using a combination of Add Field, Calculate Field, and Delete Field arcpy tools:

if fieldInfo.getFieldName(index) == "status":
    arcpy.AddField_management(layer, "stat", "TEXT", "", "", "50", "",        "NULLABLE", "NON_REQUIRED", "")                     
    arcpy.CalculateField_management(layer, "stat", "!status!", "PYTHON_9.3", "")
    arcpy.DeleteField_management(layer, "status")
12
  • 1
    I want to make the two field type same. but field.Type and AddField_management method's field_type names are not same. What to do?
    – Emi
    Commented Feb 20, 2013 at 13:43
  • @Emi, I'm not understanding what you mean by "two field type same"? You want the value in "status", to be put in a new field called "stat"?
    – artwork21
    Commented Feb 20, 2013 at 13:52
  • yes I want the value in "status", to put in the new field called "stat"
    – Emi
    Commented Feb 20, 2013 at 13:55
  • I also tried fieldInfo.addField ("status", "stat", "VISIBLE", "NONE") instead of fieldInfo.setNewName(index, "stat"). But same error happened with my code.
    – Emi
    Commented Feb 20, 2013 at 14:03
  • For rows = arcpy.UpdateCursor(inputFeatureClass) , maybe try rows = arcpy.UpdateCursor(layer)
    – artwork21
    Commented Feb 20, 2013 at 14:10
9

You can now rename a field using core ArcGIS Desktop GP tool - Alter Field (Data Management). This tool provides the ability to rename fields or rename field aliases for any geodatabase table or feature class.

This tool is available starting with 10.2.1.

1
  • 7
    This only supports certain data formats. Notably, it throws an error on shapefiles.
    – jpmc26
    Commented Sep 30, 2016 at 22:18
3

To rename a field in a table or feature class I would try the procedure described here.

  1. Start ArcMap and open the Catalog window.
  2. Locate the database that contains the table you want to alter.
  3. Right-click the table and click Properties.
  4. Click the Fields tab.
  5. Click on the existing text in the Field Name column and type a new name.

I just tested this on a file geodatabase table using ArcGIS for Desktop Standard 10.1 SP1 and it worked fine.

Unfortunately, after writing this I searched the What's New in ArcGIS 10.1 PDF file and found that this functionality may have been added at that version - still it could be worth testing the latest service pack of 10.0 to see if it was back ported there too.

3
  • 2
    I believe Emi is looking for a arcpy solution.
    – artwork21
    Commented Feb 20, 2013 at 12:51
  • 1
    Actually I am searching for arcpy solution
    – Emi
    Commented Feb 20, 2013 at 13:25
  • 1
    My mistake - I thought @Emi said "Is there any better solution ..." [my bolding].
    – PolyGeo
    Commented Feb 21, 2013 at 0:03
1

two big problems with the code:

  • if the dataType is not a FeatureLayer, the program will skip over the fieldinfo manipulations, and then try to set the value to a new/renamed field that was never created/renamed

  • The two lines:

    gValue = row.getValue("status")

    row.setValue("stat", gValue)

    have a big problem. If the earlier code worked The field "status" doesn't exist.

3
  • I checked the code and while, it is a feature layer and the feature class contains a field named "status", gives the same error
    – Emi
    Commented Feb 20, 2013 at 19:05
  • If the first line works, the second line won't. Commented Feb 20, 2013 at 19:14
  • you changed the info in the layer, but are reading and writing data from/to the featureclass Commented Feb 20, 2013 at 19:18
1

How can I map field.type to AddField_management method's field type?

This is done automatically, according to this post.

And while the field is in a middle place, then field deleted from middle and added to the last. That does not looks like as if field name is renamed. Is there any better solution that help me to do these things?

As I mentioned in your other question, your only other options are to drop and re-create the whole feature class/table or modify the column definition in the underlying DBMS.

I suppose you could delete all user columns and re-create them in the desired order but that's a little crazy. The script in this answer does exactly that, you could probably adapt it to your needs.

8
  • while the field.type="string", we have to pass "text" at AddField_management. I have checked it.
    – Emi
    Commented Feb 22, 2013 at 15:34
  • Are you sure? It works for me at 10.0 SP5.
    – blah238
    Commented Feb 22, 2013 at 16:07
  • It gives me error: arcgisscripting.ExecuteError: Failed to execute. Parameters are not valid. ERROR 000800: The value is not a member of TEXT | FLOAT | DOUBLE | SHORT | LONG | DATE | BLOB | RASTER | GUID. Failed to execute (AddField).
    – Emi
    Commented Feb 22, 2013 at 18:28
  • What version/SP are you at?
    – blah238
    Commented Feb 22, 2013 at 18:31
  • I am using 10.0. I don't know the service pack number.
    – Emi
    Commented Feb 22, 2013 at 19:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.