Select to view content in your preferred language

Calculate Field, SyntaxError invalid syntax partially working

978
4
09-01-2021 06:48 AM
DMink
by
Occasional Contributor

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:

Derek_Mink_0-1630503152044.png

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!

0 Kudos
4 Replies
DMink
by
Occasional Contributor

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?

Derek_Mink_0-1630504759970.png

 

0 Kudos
DMink
by
Occasional Contributor

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?

0 Kudos
JayantaPoddar
MVP Esteemed Contributor

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


Think Location
DMink
by
Occasional Contributor

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:

Derek_Mink_0-1630515947784.png

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?

0 Kudos