I'm attempting to run a script to calculate a field that is conditional. If float2 is available I want to calculate the ratio of float1/float2, if it is null I want to use the ratio of float1/float3, and then leave null if both float2 and float3 are null.
The script doesn't immediately fail when running, but does error out after a few thousand records with the following:
Expression:
calc(!float1!, !text1!, !float2!, !float3!)
Code Block:
def calc(f1, t1, f2, f3):
if f1 > 0 and t1 == "H":
if f2:
return f1 / f2
elif f3:
return f1 / f3
else:
return None
else:
return None
Any ideas on what is happening here or how to address it? !float1! is indeed a float, so my first thought that maybe there was some hard coded scientific notation in there doesn't seem to be an issue. Thanks!
It seems to be an error associated with that particular record. When I select on the record and do a simple calculation of float1 / float2 I get the same error. Any idea what this could be or how to fix?
OK, I'm convinced it has something to do with the scientific notation. I have some records with float1 = 10,000,000 ; 20,000,000 ; 50,000,000 and they are the only ones that fail. It seems as long as the value doesn't result in a number divisible by 10 million it works. Other records have values 10 million plus.
What's going on here?
You should use DOUBLE datatype for floating values of more than six-digits. Create a new DOUBLE field and assign the values of Field1 to it.
Could you test the following code block (Test with Field1 or the new field)?
def calc(f1, t1, f2, f3):
if f1 > 0 and t1 == "H":
if f2 > 0:
return (f1 / f2)
elif f3 > 0:
return (f1 / f3)
else:
return None
else:
return None
Thanks for your feedback Jayanta.
The feature class I'm using is created in a script from a sql server table and upon import the field is created as a float (using arcpy.XYTableToPoint_management). If I create a new field, set to double, and try to populate with the float1 value it doesn't work:
However, I don't follow how the double datatype is required here since if I run this calculation excluding only the float1 values that are multiples of 10 million it works.
EDIT: So those were the cases where it crashed, but values weren't always being calculated elsewhere. How do I create the featureclass and ensure the incoming fields are set to DOUBLE?