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 <SPAN class="operator token">=</SPAN> r"C<SPAN class="punctuation token">:</SPAN>\in_table
gm_reh_sum_mem <SPAN class="operator token">=</SPAN> arcpy<SPAN class="punctuation token">.</SPAN>management<SPAN class="punctuation token">.</SPAN>CopyRows<SPAN class="punctuation token">(</SPAN>gm_reh_sum<SPAN class="punctuation token">,</SPAN> r<SPAN class="string token">"in_memory/gm_reh_sum_mem"</SPAN><SPAN class="punctuation token">)</SPAN>
gm_reh_sum_mem <SPAN class="operator token">=</SPAN> arcpy<SPAN class="punctuation token">.</SPAN>management<SPAN class="punctuation token">.</SPAN>MakeTableView<SPAN class="punctuation token">(</SPAN>gm_reh_sum_mem<SPAN class="punctuation token">,</SPAN> <SPAN class="string token">"gm_reh_sum_mem"</SPAN><SPAN class="punctuation token">)</SPAN>
<SPAN class="keyword token">def</SPAN> <SPAN class="token function">change_fldtype</SPAN><SPAN class="punctuation token">(</SPAN>in_table<SPAN class="punctuation token">,</SPAN> in_fld<SPAN class="punctuation token">,</SPAN> out_fld_type<SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">:</SPAN> <SPAN class="comment token">##Change field type</SPAN>
arcpy<SPAN class="punctuation token">.</SPAN>management<SPAN class="punctuation token">.</SPAN>AddField<SPAN class="punctuation token">(</SPAN>in_table<SPAN class="punctuation token">,</SPAN> <SPAN class="string token">"temp"</SPAN><SPAN class="punctuation token">,</SPAN> out_fld_type<SPAN class="punctuation token">)</SPAN>
arcpy<SPAN class="punctuation token">.</SPAN>management<SPAN class="punctuation token">.</SPAN>CalculateField<SPAN class="punctuation token">(</SPAN>in_table<SPAN class="punctuation token">,</SPAN> <SPAN class="string token">"temp"</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="string token">"[{}]"</SPAN><SPAN class="punctuation token">.</SPAN>format<SPAN class="punctuation token">(</SPAN>in_fld<SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">)</SPAN>
arcpy<SPAN class="punctuation token">.</SPAN>AddMessage<SPAN class="punctuation token">(</SPAN><SPAN class="punctuation token">[</SPAN>x<SPAN class="punctuation token">.</SPAN>name <SPAN class="keyword token">for</SPAN> x <SPAN class="keyword token">in</SPAN> arcpy<SPAN class="punctuation token">.</SPAN>ListFields<SPAN class="punctuation token">(</SPAN>in_table<SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">]</SPAN><SPAN class="punctuation token">)</SPAN> <SPAN class="comment token">##DEBUG##</SPAN>
arcpy<SPAN class="punctuation token">.</SPAN>management<SPAN class="punctuation token">.</SPAN>DeleteField<SPAN class="punctuation token">(</SPAN>in_table<SPAN class="punctuation token">,</SPAN> in_fld<SPAN class="punctuation token">)</SPAN>
arcpy<SPAN class="punctuation token">.</SPAN>AddMessage<SPAN class="punctuation token">(</SPAN>in_fld<SPAN class="punctuation token">)</SPAN>
arcpy<SPAN class="punctuation token">.</SPAN>AddMessage<SPAN class="punctuation token">(</SPAN><SPAN class="punctuation token">[</SPAN>x<SPAN class="punctuation token">.</SPAN>name <SPAN class="keyword token">for</SPAN> x <SPAN class="keyword token">in</SPAN> arcpy<SPAN class="punctuation token">.</SPAN>ListFields<SPAN class="punctuation token">(</SPAN>in_table<SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">]</SPAN><SPAN class="punctuation token">)</SPAN> <SPAN class="comment token">##DEBUG##</SPAN>
arcpy<SPAN class="punctuation token">.</SPAN>management<SPAN class="punctuation token">.</SPAN>AddField<SPAN class="punctuation token">(</SPAN>in_table<SPAN class="punctuation token">,</SPAN> in_fld<SPAN class="punctuation token">,</SPAN> out_fld_type<SPAN class="punctuation token">)</SPAN>
arcpy<SPAN class="punctuation token">.</SPAN>management<SPAN class="punctuation token">.</SPAN>CalculateField<SPAN class="punctuation token">(</SPAN>in_table<SPAN class="punctuation token">,</SPAN> in_fld<SPAN class="punctuation token">,</SPAN> <SPAN class="string token">"[temp]"</SPAN><SPAN class="punctuation token">)</SPAN>
arcpy<SPAN class="punctuation token">.</SPAN>management<SPAN class="punctuation token">.</SPAN>DeleteField<SPAN class="punctuation token">(</SPAN>in_table<SPAN class="punctuation token">,</SPAN> <SPAN class="string token">"temp"</SPAN><SPAN class="punctuation token">)</SPAN>
change_fldtype<SPAN class="punctuation token">(</SPAN>gm_reh_sum_mem<SPAN class="punctuation token">,</SPAN> <SPAN class="string token">"Length_Cleaned__Feet_"</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="string token">"DOUBLE"</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="line-numbers-rows"><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN></SPAN>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).<SPAN class="line-numbers-rows"><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN></SPAN>
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!