I have the following code (excerpt from script), I intend it to change the field format in a table from a string type to a numerical type.
gm_reh_sum = r"C:\in_table
gm_reh_sum_mem = arcpy.management.CopyRows(gm_reh_sum, r"in_memory/gm_reh_sum_mem")
gm_reh_sum_mem = arcpy.management.MakeTableView(gm_reh_sum_mem, "gm_reh_sum_mem")
def change_fldtype(in_table, in_fld, out_fld_type): ##Change field type
arcpy.management.AddField(in_table, "temp", out_fld_type)
arcpy.management.CalculateField(in_table, "temp", "[{}]".format(in_fld))
arcpy.AddMessage([x.name for x in arcpy.ListFields(in_table)]) ##DEBUG##
arcpy.management.DeleteField(in_table, in_fld)
arcpy.AddMessage(in_fld)
arcpy.AddMessage([x.name for x in arcpy.ListFields(in_table)]) ##DEBUG##
arcpy.management.AddField(in_table, in_fld, out_fld_type)
arcpy.management.CalculateField(in_table, in_fld, "[temp]")
arcpy.management.DeleteField(in_table, "temp")
change_fldtype(gm_reh_sum_mem, "Length_Cleaned__Feet_", "DOUBLE")
However, when I run it, for some reason, the delete field in the line arcpy.management.DeleteField(in_table, in_fld) does not delete the field, causing the script to fail when it then tries to add a new field of the new data type with the same name. This is the error that I get (I've also included the debug lines printed before the error message).
Running script WorkSummaryDataImport...
[u'OBJECTID', u'Date_of_Repair__MM_DD_YYYY_', u'Main_Line_Asset_ID', u'Mechanical_Heavy_Sewer_Cleaning___Root_Removal__Yes_No_', u'Length_Cleaned__Feet_', u'Pipe_Burst__Yes_No_', u'CIPP__Yes_No_', u'Open_Cut__Yes_No_', u'Length_Repaired__Feet_', u'Point_Repair_Location__LF_from_US_MH_', u'Point_Repair_Material__PACP_Code_', u'Point_Repair_Greater_than_16_feet_deep___Yes_No_', u'Point_Repair_Length__Feet_', u'Repair_Diameter__inches_', u'Contractor_Comments', u'temp']
Length_Cleaned__Feet_
[u'OBJECTID', u'Date_of_Repair__MM_DD_YYYY_', u'Main_Line_Asset_ID', u'Mechanical_Heavy_Sewer_Cleaning___Root_Removal__Yes_No_', u'Pipe_Burst__Yes_No_', u'CIPP__Yes_No_', u'Open_Cut__Yes_No_', u'Length_Repaired__Feet_', u'Point_Repair_Location__LF_from_US_MH_', u'Point_Repair_Material__PACP_Code_', u'Point_Repair_Greater_than_16_feet_deep___Yes_No_', u'Point_Repair_Length__Feet_', u'Repair_Diameter__inches_', u'Contractor_Comments', u'temp', u'Length_Cleaned__Feet_']
Failed script WorkSummaryDataImport...
Traceback (most recent call last):
File "C:\Users\zieglerhm\Documents\ArcGIS\Projects\CW2020_IRP_DataProcessing\Script\WorkSummaryDataImport_v0.3.py", line 179, in <module>
change_fldtype(gm_reh_sum_mem, "Length_Cleaned__Feet_", "DOUBLE")
File "C:\Users\zieglerhm\Documents\ArcGIS\Projects\CW2020_IRP_DataProcessing\Script\WorkSummaryDataImport_v0.3.py", line 20, in change_fldtype
arcpy.management.AddField(in_table, in_fld, out_fld_type)
File "c:\program files (x86)\arcgis\desktop10.3\arcpy\arcpy\management.py", line 3246, in AddField
raise e
ExecuteError: Failed to execute. Parameters are not valid.
ERROR 000012: Length_Cleaned__Feet_ already exists
Failed to execute (AddField).
Note that before I try to change the field format, I copy the table to memory and then create a table view object on which to operate (this is necessary for later portions of my script not shown here).
Thank you for any help or pointers in the right direction!
Solved! Go to Solution.
I think you have run into a bug. Using Copy Rows and Make Table View, I can replicate your problem. Using Copy Features and Make Feature Layer, your code works fine. So, why does it work with feature layers and not table views? That is where I think the bug comes into play.
So, seeing there is a bug, what can you do about it? In this case, the workaround is fairly simple. Instead of passing the table view, pass the actual name of the in-memory table. Right now, your code overwrites gm_reh_sum_mem with the result from creating the table view. I recommend having different variables to hold the in-memory table result and make table view result, and then pass the in-memory table name to your function.
I think you have run into a bug. Using Copy Rows and Make Table View, I can replicate your problem. Using Copy Features and Make Feature Layer, your code works fine. So, why does it work with feature layers and not table views? That is where I think the bug comes into play.
So, seeing there is a bug, what can you do about it? In this case, the workaround is fairly simple. Instead of passing the table view, pass the actual name of the in-memory table. Right now, your code overwrites gm_reh_sum_mem with the result from creating the table view. I recommend having different variables to hold the in-memory table result and make table view result, and then pass the in-memory table name to your function.
Yes, this is what I ended up having to do, thank you!