Select to view content in your preferred language

Arcpy Add Field only outputing as text data ype

802
2
01-01-2023 06:12 PM
Outraged_Oregano
Emerging Contributor

I created a simple tool that runs a batch area calculation and then calculates the estimate volume from this area calculation based on some literature. I made this tool a few months ago and it was running fine. For some reason, the other night, my tool is only wanting to output my volume field as a text data type. I have absolutely no idea why this is happening. I don't have the word text anywhere in my tool aside from the end to GetParameterAsText.

My tool has the ability to update fields with the name if they already exist, or add them if they don't exist. I have tried this with deleting the Area and Volume fields. In this case, the only fields are OBJECTID, Name (name of the site, which is text), Shape_Length, Shape_Area. The new fields outputted are still coming as text.

I have also tried this with leaving the fields in and letting the tool update them instead, and the fields are still coming out as text, even if they were float before I ran the tool.

I have tried both:

arcpy.management.AddField(in_table = x, field_name = 'EstVol', field_type = 'FLOAT')

arcpy.management.CalculateField(in_table = x, field = 'EstVol', expression = '10**(.9*math.log10(!Area!))', field_type = 'FLOAT')

and

arcpy.management.CalculateField(in_table = x, field = 'EstVol', expression = '10**(.9*math.log10(!Area!))', field_type = 'FLOAT') without the add field as it seems the calculate field documentation will create the field if it doesn't exist (so the add field line is actually redundant above).

I've written these lines in a notebook, and they are correctly outputting a field which is float data type. I haven't changed anything in the script for my tool since I created it. I know for a fact that it worked in the past, because older sites I ran this tool on have the field as float and I did not calculate these without running the tool (and without using it in a notebook).

I am at a loss. I need to get the sum of these values and I've had some success with just using a search cursor to append them to a list and then make the list items a float, but I'm starting to run into other issues because these are text values.

At this point I'm convinced this is an esri glitch.

I've attached the entire script. It's not very pretty, it was my first tool ever. But hopefully there's enough clarity in there to find a solution or to agree with my conclusion this makes zero sense to output as a text.

0 Kudos
2 Replies
GoWest
by
Regular Contributor

Which line in your program is failing and producing incorrect results?
Try submitting a smaller program by deleting the sections which are not causing the problem.

Which section did you want us to examine?

if len(param1) == 0:

or

if len(param0) == 0:

 Also, tell us what parameters you used for param0, param1, param2,...

0 Kudos
AlfredBaldenweck
MVP Regular Contributor

There's a solid chance that it has to do with if your field gets deleted or not. Close and Re-open Pro, give the tool a shot without deleting any fields, and then close and re-open pro again but give it a shot at deleting.

Re: Calculate Field not working when iterating thr... - Esri Community

0 Kudos