Take the 2-minute tour ×
Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. It's 100% free, no registration required.

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:

EDIT:

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)

Question: 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?

Thanks

share|improve this question
    
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 Feb 20 '13 at 13:14
2  
setNewName (index, new_field_name), must be for only setting new field names, not renaming existing ones. –  artwork21 Feb 20 '13 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 Feb 20 '13 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 Feb 21 '13 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 Feb 22 '13 at 16:05

5 Answers 5

up vote 8 down vote accepted

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

if fieldInfo.getFieldName(index)=="status":

            # Process: Add Field
            arcpy.AddField_management(layer, "stat", "TEXT", "", "", "50", "",        "NULLABLE", "NON_REQUIRED", "")

            # Process: Calculate Field                            
            arcpy.CalculateField_management(layer, "stat", "!status!", "PYTHON_9.3", "")

            # Process: Delete Field                 
            arcpy.DeleteField_management(layer, "status")
share|improve this answer
    
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 Feb 20 '13 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 Feb 20 '13 at 13:52
    
yes I want the value in "status", to put in the new field called "stat" –  Emi Feb 20 '13 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 Feb 20 '13 at 14:03
1  
+1 Simple is best –  Aaron Feb 20 '13 at 14:26

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.

share|improve this answer

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.

share|improve this answer
    
while the field.type="string", we have to pass "text" at AddField_management. I have checked it. –  Emi Feb 22 '13 at 15:34
    
Are you sure? It works for me at 10.0 SP5. –  blah238 Feb 22 '13 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 Feb 22 '13 at 18:28
    
What version/SP are you at? –  blah238 Feb 22 '13 at 18:31
    
I am using 10.0. I don't know the service pack number. –  Emi Feb 22 '13 at 19:46

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.

share|improve this answer
    
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 Feb 20 '13 at 19:05
    
If the first line works, the second line won't. –  mhoran_psprep Feb 20 '13 at 19:14
    
you changed the info in the layer, but are reading and writing data from/to the featureclass –  mhoran_psprep Feb 20 '13 at 19:18

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.

share|improve this answer
    
I believe Emi is looking for a arcpy solution. –  artwork21 Feb 20 '13 at 12:51
    
Actually I am searching for arcpy solution –  Emi Feb 20 '13 at 13:25
1  
My mistake - I thought @Emi said "Is there any better solution ..." [my bolding]. –  PolyGeo Feb 21 '13 at 0:03

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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